<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/dxl/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/dxl/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/dxl/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/dxl/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/dxl/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/dxl/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/dxl/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/dxl/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/dxl/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="面试题,">










<meta name="description" content="第一章1、为什么学习python 1家里有在这个IT圈子里面，也想让我接触这个圈子，然后给我建议学的Python，然后自己通过百度和向有学过Python的同学了解了Python，Python这门语言，入门比较简单，它简单易学，生态圈比较强大，涉及的地方比较多，特别是在人智能，和数据分析这方面。在未来我觉得是往自动化，人工智能这方面发展的，所以学习了Python  2、通过什么途径学习的Python">
<meta name="keywords" content="面试题">
<meta property="og:type" content="article">
<meta property="og:title" content="绿皮书01">
<meta property="og:url" content="http://yoursite.com/2019/09/20/【面试题】01-1绿皮书一/index.html">
<meta property="og:site_name" content="我的快乐时光">
<meta property="og:description" content="第一章1、为什么学习python 1家里有在这个IT圈子里面，也想让我接触这个圈子，然后给我建议学的Python，然后自己通过百度和向有学过Python的同学了解了Python，Python这门语言，入门比较简单，它简单易学，生态圈比较强大，涉及的地方比较多，特别是在人智能，和数据分析这方面。在未来我觉得是往自动化，人工智能这方面发展的，所以学习了Python  2、通过什么途径学习的Python">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://yoursite.com/2019/09/20/【面试题】01-1绿皮书一/assets/1566787167007.png">
<meta property="og:updated_time" content="2019-10-10T13:59:43.860Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="绿皮书01">
<meta name="twitter:description" content="第一章1、为什么学习python 1家里有在这个IT圈子里面，也想让我接触这个圈子，然后给我建议学的Python，然后自己通过百度和向有学过Python的同学了解了Python，Python这门语言，入门比较简单，它简单易学，生态圈比较强大，涉及的地方比较多，特别是在人智能，和数据分析这方面。在未来我觉得是往自动化，人工智能这方面发展的，所以学习了Python  2、通过什么途径学习的Python">
<meta name="twitter:image" content="http://yoursite.com/2019/09/20/【面试题】01-1绿皮书一/assets/1566787167007.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/dxl/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/20/【面试题】01-1绿皮书一/">





  <title>绿皮书01 | 我的快乐时光</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/dxl/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">我的快乐时光</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/dxl/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/dxl/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/dxl/2019/09/20/【面试题】01-1绿皮书一/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content>
      <meta itemprop="description" content>
      <meta itemprop="image" content="/dxl/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="我的快乐时光">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">绿皮书01</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-20T20:05:12+08:00">
                2019-09-20
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/dxl/categories/面试题/" itemprop="url" rel="index">
                    <span itemprop="name">面试题</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/dxl/2019/09/20/【面试题】01-1绿皮书一/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/dxl/2019/09/20/【面试题】01-1绿皮书一/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="第一章"><a href="#第一章" class="headerlink" title="第一章"></a>第一章</h2><p>1、为什么学习python</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">家里有在这个IT圈子里面，也想让我接触这个圈子，然后给我建议学的Python，然后自己通过百度和向有学过Python的同学了解了Python，Python这门语言，入门比较简单，它简单易学，生态圈比较强大，涉及的地方比较多，特别是在人智能，和数据分析这方面。在未来我觉得是往自动化，人工智能这方面发展的，所以学习了Python</span><br></pre></td></tr></table></figure>

<p>2、通过什么途径学习的Python？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">书籍：python核心编程</span><br><span class="line">	  廖雪峰的python教程</span><br></pre></td></tr></table></figure>

<p>3、公司线上和开发环境使用的什么系统？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">linux 系统</span><br></pre></td></tr></table></figure>

<p>4、Python和Java、PHP、C、C#、C++等其他语言的对比？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1、python中的变量不需要声明（与Java相比）</span><br><span class="line">2、PHP python比较多元化</span><br><span class="line">3、python与比较</span><br><span class="line">	 Python 和 C Python这门语言是由C开发而来</span><br><span class="line">	 对于使用：Python的类库齐全并且使用简洁，如果要实现同样的功  能，Python 10行代码可以解决，C可能就需要100行甚至更多.</span><br><span class="line">　　  对于速度：Python的运行速度相较与C，绝逼是慢了</span><br></pre></td></tr></table></figure>

<p>5、简述解释型和编译型编程语言？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">解释型：就是边解释边执行（Python，php）</span><br><span class="line">编译型：编译后再执行（c、java、c#）</span><br></pre></td></tr></table></figure>

<p>6、Python解释器种类以及特点</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#CPython</span></span><br><span class="line">	是官方版本的解释器：CPython。是使用C语言开发的</span><br><span class="line"><span class="comment">#IPython</span></span><br><span class="line">	IPython是基于CPython之上的一个交互式解释器，也就是说，IPython只是在交互方式上有所增强，但是执行Python代码的功能和CPython是完全一样的</span><br><span class="line"><span class="comment">#PyPy</span></span><br><span class="line">	由Python写的解释器，它的执行速度是最快。PyPy采用JIT技术，对Python代码进行动态编译（注意不是解释），运行速度比cpython快</span><br><span class="line"><span class="comment">#Jython</span></span><br><span class="line">	Jython是运行在Java平台上的Python解释器，可以直接把Python代码编译成Java字节码执行</span><br><span class="line"><span class="comment">#IronPython</span></span><br><span class="line">	IronPython和Jython类似，只不过IronPython是运行在.Net平台上的Python解释器，可以直接把Python代码编译成.Net的字节码</span><br><span class="line">总结：</span><br><span class="line">	Python的解释器很多，但使用最广泛的还是CPython。如果要和Java或.Net平台交互，最好的办法不是用Jython或IronPython，而是通过网络调用来交互，确保各程序</span><br></pre></td></tr></table></figure>

<p>7、位和字节的关系？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">8位等于1字节</span><br></pre></td></tr></table></figure>

<p>8、b、B、KB、MB、GB 的关系？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">8bit = 1 B</span><br><span class="line">1024B = 1KB</span><br><span class="line">1024KB = 1MB</span><br><span class="line">1024MB = 1GB</span><br></pre></td></tr></table></figure>

<p> 9、请至少列举5个 PEP8 规范（越多越好）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">	<span class="comment">#1、使用4个空格而不是tab键进行缩进。</span></span><br><span class="line">	<span class="comment">#2、每行长度不能超过79</span></span><br><span class="line">	<span class="number">3</span>、使用空行来间隔函数和类，以及函数内部的大块代码</span><br><span class="line">	<span class="comment">#4、必要时候，在每一行下写注释</span></span><br><span class="line">	<span class="number">5</span>、使用文档注释，写出函数注释</span><br><span class="line">	<span class="number">6</span>、在操作符和逗号之后使用空格，但是不要在括号内部使用</span><br><span class="line">	<span class="number">7</span>、命名类和函数的时候使用一致的方式，比如使用CamelCase来命名类，</span><br><span class="line">       使用lower_case_with_underscores来命名函数和方法</span><br><span class="line">	<span class="number">8</span>、在类中总是使用self来作为默认</span><br><span class="line">    <span class="number">9</span>、尽量不要使用魔法方法</span><br><span class="line">	<span class="comment">#10、默认使用UTF-8，甚至ASCII作为编码方式</span></span><br><span class="line">	<span class="number">11</span>、换行可以使用反斜杠，最好使用圆括号。</span><br><span class="line">	<span class="comment">#12、不要在一句import中多个库，</span></span><br><span class="line">空格的使用：</span><br><span class="line">	各种右括号前不要加空格。</span><br><span class="line">	<span class="comment">#逗号、冒号、分号前不要加空格。</span></span><br><span class="line">		函数的左括号前 不要加空格。如Func(<span class="number">1</span>)</span><br><span class="line">	序列的左括号前不要加空格。如list[<span class="number">2</span>]</span><br><span class="line">	操作符左右各加一个空格，不要为了对齐增加空格</span><br><span class="line">	函数默认参数使用的赋值符左右省略空格</span><br><span class="line">	不要将多句语句写在同一行，尽管使用‘；’允许</span><br><span class="line">	<span class="keyword">if</span>/<span class="keyword">for</span>/<span class="keyword">while</span>语句中，即使执行语句只有一句，也必须另起一行</span><br><span class="line">   函数命名使用全部小写的方式，常量命名使用大写，类属性（方法和变量）使用小写</span><br><span class="line">类名使用大驼</span><br></pre></td></tr></table></figure>

<p>10、求结果：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="number">1</span> <span class="keyword">or</span> <span class="number">3</span>)  <span class="comment"># 1</span></span><br><span class="line">print(<span class="number">1</span> <span class="keyword">and</span> <span class="number">3</span>)  <span class="comment"># 3</span></span><br><span class="line">print(<span class="number">0</span> <span class="keyword">and</span> <span class="number">2</span> <span class="keyword">and</span> <span class="number">1</span>)  <span class="comment"># 0</span></span><br><span class="line">print(<span class="number">0</span> <span class="keyword">and</span> <span class="number">2</span> <span class="keyword">or</span> <span class="number">1</span>)  <span class="comment"># 1</span></span><br><span class="line">print(<span class="number">0</span> <span class="keyword">and</span> <span class="number">2</span> <span class="keyword">or</span> <span class="number">1</span> <span class="keyword">or</span> <span class="number">4</span>)  <span class="comment"># 1</span></span><br><span class="line">print(<span class="number">0</span> <span class="keyword">or</span> <span class="literal">False</span> <span class="keyword">and</span> <span class="number">1</span>)  <span class="comment"># Flase</span></span><br><span class="line"></span><br><span class="line">   <span class="comment"># x or y 如果 x为真，则值为x，   否则为y</span></span><br><span class="line">　　<span class="comment"># x and y 如果 x 为真，则值为 y，否则为 x</span></span><br></pre></td></tr></table></figure>

<p>11、ascii、unicode、utf-8、gbk 区别</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">python2内容进行编码（默认ascii）,而python3对内容进行编码的默认为utf<span class="number">-8</span>。</span><br><span class="line"></span><br><span class="line">ascii   最多只能用<span class="number">8</span>位来表示（一个字节），即：<span class="number">2</span>**<span class="number">8</span> = <span class="number">256</span>，所以，ASCII码最多只能表示 <span class="number">256</span> 个符号。</span><br><span class="line">unicode   万国码，任何一个字符==四个字节</span><br><span class="line">utf<span class="number">-8</span>     万国码的升级版  一个中文字符==三个字节   英文是一个字节  欧洲的是 <span class="number">2</span>个字节</span><br><span class="line">gbk       国内版本  一个中文字符==<span class="number">2</span>个字节   英文是一个字节</span><br><span class="line">gbk 转 utf<span class="number">-8</span>  需通过媒介 unicode</span><br></pre></td></tr></table></figure>

<p>12、字节码和机器码的区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">什么是机器码</span><br><span class="line">	机器码(machine code)，学名机器语言指令，有时也被称为原生码（Native Code），是电脑的CPU可直接解读的数据。通常意义上来理解的话，机器码就是计算机可以直接执行，并且执行速度最快的代码。</span><br><span class="line">什么是字节码：</span><br><span class="line">    字节码（Bytecode）是一种包含执行程序、由一序列 op 代码/数据对 组成的二进制文件。</span><br><span class="line">    字节码是一种中间码，它比机器码更抽象，需要直译器转译后才能成为机器码的中间代码。</span><br></pre></td></tr></table></figure>

<p>13、三元运算写法和应用场景</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">语法：</span><br><span class="line">	结果： <span class="keyword">if</span>条件 <span class="keyword">else</span>：结果</span><br><span class="line">eg:</span><br><span class="line">	result=<span class="string">'gt'</span> <span class="keyword">if</span> <span class="number">1</span>&gt;<span class="number">3</span> <span class="keyword">else</span> <span class="string">'lt'</span> <span class="comment">#应用场景一、简化if语句</span></span><br></pre></td></tr></table></figure>

<p>14、Python3和Python2的区别</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、打印：</span><br><span class="line">    python <span class="number">2</span> ： <span class="keyword">print</span> <span class="string">'lili'</span></span><br><span class="line">    python <span class="number">3</span> : <span class="keyword">print</span> (<span class="string">'lili'</span>)</span><br><span class="line"><span class="number">2</span>、内涵：</span><br><span class="line">	python2：源码的重复较多，语法不清楚，掺杂着一些c/php/java 的一些陋习</span><br><span class="line">    python3:代码几乎是重构过得，规范。清晰。优美</span><br><span class="line"><span class="number">3</span>、输出中文的区别</span><br><span class="line">    python2：要输出中文 需加 <span class="comment"># -*- encoding:utf-8 -*-</span></span><br><span class="line">    Python3 ： 直接搞</span><br><span class="line"><span class="number">4</span>、input不同</span><br><span class="line">	python2 ：raw_input（字符串）</span><br><span class="line">	python3 ：input 统一使用input函数</span><br><span class="line"><span class="number">5</span>、指定字节</span><br><span class="line">    python2在编译安装时，可以通过参数-----enable-unicode=ucs2 或-----enable-unicode=ucs4分别用于指定使用<span class="number">2</span>个字节、<span class="number">4</span>个字节表示一个unicode；</span><br><span class="line">    python3无法进行选择，默认使用 ucs4</span><br><span class="line">    查看当前python中表示unicode字符串时占用的空间：</span><br><span class="line">    impor sys</span><br><span class="line">    <span class="keyword">print</span>（sys.maxunicode）</span><br><span class="line">    <span class="comment">#如果值是65535，则表示使用usc2标准，即：2个字节表示</span></span><br><span class="line">    <span class="comment">#如果值是1114111，则表示使用usc4标准，即：4个字节表示</span></span><br><span class="line"><span class="number">6</span>、range</span><br><span class="line">    py2：xrange（可迭代对象）  range（列表）</span><br><span class="line">    py3：range  统一使用range，Python3中range的机制也进行修改并提高了大数据集生成效率</span><br><span class="line"><span class="number">7</span>、在包的集合里</span><br><span class="line">	包：一群模块文件的集合 + __init__</span><br><span class="line">	区别：py2 ： 必须有__init__</span><br><span class="line">　　　	   py3：不是必须的了</span><br><span class="line"><span class="number">8</span>、不相等操作符<span class="string">"&lt;&gt;"</span>被Python3废弃，统一使用<span class="string">"!="</span></span><br><span class="line"><span class="number">9</span>；long整数类型被Python3废弃，统一使用int</span><br><span class="line"><span class="number">10</span> 迭代器iterator的next()函数被Python3废弃，统一使用next(iterator)</span><br><span class="line"><span class="number">11</span>、异常StandardError 被Python3废弃，统一使用Exception</span><br><span class="line"><span class="number">12</span>、字典变量的has_key函数被Python废弃，统一使用<span class="keyword">in</span>关键词</span><br><span class="line"><span class="number">13</span>、file函数被Python3废弃，统一使用open来处理文件，可以通过io.IOBase检查文件类型</span><br></pre></td></tr></table></figure>

<p>15、py2项目转成py3项目</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2to3.py -w example.py</span><br></pre></td></tr></table></figure>

<p>16、用一行代码实现数值交换</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = 1 </span><br><span class="line">b = 2</span><br><span class="line">a, b = b, a</span><br></pre></td></tr></table></figure>

<p>17、Python3和Python2中int和long区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在python3里，只有一种整数类型int,大多数情况下，和python２中的长整型类似。</span><br></pre></td></tr></table></figure>

<p>18、xrange和range的区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">python2中:</span><br><span class="line">     xrange内存性能更好，xrange用法与range完全相同，range一个生成list对象，xrange是生成器</span><br><span class="line">Python 3 中，</span><br><span class="line">	range() 是像 xrange() 那样实现，xrange()被抛弃。</span><br></pre></td></tr></table></figure>

<p>19、如何实现字符串的反转，如：name =“李白123” name =“321白李”</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">方法一:</span><br><span class="line">    name =<span class="string">"李白123"</span></span><br><span class="line">    name = name[::<span class="number">-1</span>]</span><br><span class="line">    print(name)</span><br><span class="line">方法二：</span><br><span class="line">    name =<span class="string">"aassss"</span></span><br><span class="line">    name_list = list(name)</span><br><span class="line">    name_list.reverse()</span><br><span class="line">    name = <span class="string">""</span>.join(name_list)</span><br><span class="line">    print(name)</span><br></pre></td></tr></table></figure>

<p>20、文件操作时：xreadlines和readlines的区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">readlines     返回一个列表</span><br><span class="line">xreadlines   返回一个生成器</span><br></pre></td></tr></table></figure>

<p>21、 列列举布尔值为False的常见值？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">0，“”，&#123;&#125;，[],（），set（）</span><br><span class="line">0 Flask 负数 不成立的表达式  None 等</span><br></pre></td></tr></table></figure>

<p>22、字符串、列表、元组、字典每个常用的5个方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">字符串：</span><br><span class="line">	find index split captalize upper lower format split strip</span><br><span class="line">字典：</span><br><span class="line">	clear pop popitem <span class="keyword">del</span> </span><br><span class="line">	keys items values </span><br><span class="line">	get </span><br><span class="line">列表：</span><br><span class="line">	appened insert extend</span><br><span class="line">	pop remove clear <span class="keyword">del</span> </span><br><span class="line">	sort index</span><br><span class="line">元组：</span><br><span class="line">	len max min tuple count  </span><br><span class="line">集合</span><br><span class="line">	set add pop  discard remove clear</span><br><span class="line">    union difference intersection</span><br></pre></td></tr></table></figure>

<p>23 is和==的区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">is 内存地址相等</span><br><span class="line">== 内容相等</span><br></pre></td></tr></table></figure>

<p>24 1、2、3、4、5 能组成多少个互不相同且无重复的三位数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import itertools</span><br><span class="line">print(len(list(itertools.permutations(&apos;12345&apos;, 3))))  # 60</span><br></pre></td></tr></table></figure>

<p>25、 什么是反射？以及应⽤用场景？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">反射的核心本质就是以字符串的形式去导入个模块，利用字符串的形式去执行函数。</span><br><span class="line">Django中的 CBV就是基于反射实现的。</span><br></pre></td></tr></table></figure>

<p>26、谈谈Python的深浅拷贝？以及实现方法和应用场景</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">浅拷贝只是增加了一个指针指向一个存在的地址，可变数据类、不可变数据类型共用一块内存</span><br><span class="line">深拷贝是增加一个指针并且开辟了新的内存，这个增加的指针指向这个新的内存，可变数据类重新开辟空间、不可变数据类型共用一块内存</span><br><span class="line">采用浅拷贝的情况，释放内存，会释放同一内存，深拷贝就不会出现释放同一内存的错误</span><br></pre></td></tr></table></figure>

<p>27、Python垃圾回收机制</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">引用计数</span><br><span class="line">标记清除</span><br><span class="line">分代回收</span><br></pre></td></tr></table></figure>

<p>28、Python的可变类型和不可变类型</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">可变数据类型：列表、字典、可变集合</span><br><span class="line">不可变数据类型：数字、字符串、元组、不可变集合</span><br></pre></td></tr></table></figure>

<p>29、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">v =dict.fromkeys([<span class="string">"k1"</span>,<span class="string">"k2"</span>],[])</span><br><span class="line">v[<span class="string">"k1"</span>].append(<span class="number">66</span>)</span><br><span class="line">print(v) <span class="comment">#&#123;'k1': [66], 'k2': [66]&#125;</span></span><br><span class="line">v[<span class="string">"k1"</span>] = <span class="number">777</span></span><br><span class="line">print(v) <span class="comment">#&#123;'k1': 777, 'k2': [66]&#125;</span></span><br></pre></td></tr></table></figure>

<p>30、一行代码实现删除列表中重复的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">list(set([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">45</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">343</span>, <span class="number">2</span>, <span class="number">2</span>]))</span><br></pre></td></tr></table></figure>

<p>31、如何实现 “1,2,3” 变成 [‘1’,’2’,’3’]</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">list(&quot;1,2,3&quot;.split(&apos;,&apos;))</span><br></pre></td></tr></table></figure>

<p>32、如何实现[‘1’,’2’,’3’]变成[1,2,3]、</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[int(x) <span class="keyword">for</span> x <span class="keyword">in</span> [<span class="string">'1'</span>,<span class="string">'2'</span>,<span class="string">'3'</span>]]</span><br></pre></td></tr></table></figure>

<p>33、a = [1,2,3] 和 b = [(1),(2),(3) ] 以及  c= [(1,),(2,),(3,) ] 的区别？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a,b一样</span><br><span class="line">c是列表套元组</span><br></pre></td></tr></table></figure>

<p>34、如何用一行代码生成[1,4,9,16,25,36,49,64,81,100]</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[i*i for i in range(1,11)]</span><br></pre></td></tr></table></figure>

<p>35、常用的字符串格式化有哪几种</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">format %s f</span><br></pre></td></tr></table></figure>

<p>36、什么是断言？应用场景？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">条件成立（布尔值为True）则继续往下，否则跑出异常，</span><br><span class="line">一般用于：满足某个条件之后，才能执行，否则应该跑出异常。</span><br></pre></td></tr></table></figure>

<p>37、有两个字符串列表a和b,每个字符串都是由逗号分隔的一些字符：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">a =[</span><br><span class="line">    <span class="string">"a,1"</span>,</span><br><span class="line">    <span class="string">"b,3,22"</span>,</span><br><span class="line">    <span class="string">"c,3,4"</span></span><br><span class="line">]</span><br><span class="line">b =[</span><br><span class="line">    <span class="string">"a,2"</span>,</span><br><span class="line">    <span class="string">"b,1"</span>,</span><br><span class="line">    <span class="string">"d,2"</span>,</span><br><span class="line">]</span><br><span class="line"><span class="comment">#如何得到c</span></span><br><span class="line"><span class="comment"># c =[</span></span><br><span class="line"><span class="comment">#     "a,1,2",</span></span><br><span class="line"><span class="comment">#     "b,3,22,1",</span></span><br><span class="line"><span class="comment">#     "c,3,4",</span></span><br><span class="line"><span class="comment">#     "d,2"</span></span><br><span class="line"><span class="comment"># ]</span></span><br><span class="line">dic =&#123;&#125;</span><br><span class="line"><span class="keyword">for</span> con <span class="keyword">in</span> a:</span><br><span class="line">    dic[con[<span class="number">0</span>]] =con</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> bn <span class="keyword">in</span> b:</span><br><span class="line">    <span class="keyword">if</span> bn[<span class="number">0</span>] <span class="keyword">in</span> dic:</span><br><span class="line">        dic[bn[<span class="number">0</span>]]+=bn[<span class="number">1</span>:]</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        dic[bn[<span class="number">0</span>]] = bn</span><br><span class="line">c = list(dic.values())</span><br><span class="line">print(c)</span><br></pre></td></tr></table></figure>

<p>38、有一个多层嵌套的列表A=[1,2,[3,4,[“4”,]]],请用一段代码遍历A中的每一个元素并打印出来</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">A=[<span class="number">1</span>,<span class="number">2</span>,[<span class="number">3</span>,<span class="number">4</span>,[<span class="string">"4"</span>,]]]</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get</span><span class="params">(A)</span>:</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> A:</span><br><span class="line">        <span class="keyword">if</span> type(i) == list:</span><br><span class="line">            get(i)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(i)</span><br><span class="line">get(A）</span><br></pre></td></tr></table></figure>

<p>39、a = range(10),a[::-3]   结果：range（9，-1,-3）</p>
<p>40、下面那个命令可以从虚拟环境中退出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">deactivate</span><br></pre></td></tr></table></figure>

<p>41、将列表内的元素,根据位数合并成字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">lst = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">8</span>,<span class="number">16</span>,<span class="number">32</span>,<span class="number">64</span>,<span class="number">128</span>,<span class="number">256</span>,<span class="number">512</span>,<span class="number">1024</span>,<span class="number">32769</span>,<span class="number">65536</span>,<span class="number">4294967296</span>]</span><br><span class="line"><span class="comment"># 结果</span></span><br><span class="line">&#123;<span class="number">1</span>: [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>], <span class="number">2</span>: [<span class="number">16</span>, <span class="number">32</span>, <span class="number">64</span>], <span class="number">3</span>: [<span class="number">128</span>, <span class="number">256</span>, <span class="number">512</span>], <span class="number">4</span>: [<span class="number">1024</span>], <span class="number">5</span>: [<span class="number">32769</span>, <span class="number">65536</span>], <span class="number">10</span>: [<span class="number">4294967296</span>]&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#解题</span></span><br><span class="line">lst = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">8</span>,<span class="number">16</span>,<span class="number">32</span>,<span class="number">64</span>,<span class="number">128</span>,<span class="number">256</span>,<span class="number">512</span>,<span class="number">1024</span>,<span class="number">32769</span>,<span class="number">65536</span>,<span class="number">4294967296</span>]</span><br><span class="line">dic=&#123;&#125;</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> lst:</span><br><span class="line">    len_i=len(str(i))</span><br><span class="line">    dic.setdefault(len_i,[]).append(i)</span><br><span class="line">print(dic)</span><br><span class="line"><span class="comment">#setdefault有则不变，无则添加</span></span><br></pre></td></tr></table></figure>

<p>42、用尽量简洁的方法将二维数组合并成一维数组</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">lst = [[1,2,3],[4,5,6],[7,8,9]]</span><br><span class="line">for l in lst:</span><br><span class="line">    # ll+=l</span><br><span class="line">    ll.extend(l)</span><br><span class="line">print(ll)</span><br></pre></td></tr></table></figure>

<p>43将列表按照下列规则排序：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">正数在前，负数在后</span><br><span class="line">正数从小到大</span><br><span class="line">负数从大到小</span><br><span class="line">lis = [<span class="number">7</span>,<span class="number">-8</span>,<span class="number">5</span>,<span class="number">4</span>,<span class="number">0</span>,<span class="number">-2</span>,<span class="number">-5</span>]</span><br><span class="line">lis=sorted(lis,key=<span class="keyword">lambda</span> x:(x&lt;<span class="number">0</span>,abs(x)))</span><br><span class="line"><span class="comment">#分两步理解</span></span><br></pre></td></tr></table></figure>

<p>44、解决哈希冲突的算法有哪几种？分别有什么特点？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">哈希冲突参考</span><br><span class="line">    开放定址法</span><br><span class="line">    再哈希法</span><br><span class="line">    链地址法</span><br><span class="line">    建立公共溢出区</span><br></pre></td></tr></table></figure>

<p>45、简述python字符串的驻留机制</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">相同对象的引用都指向内存中的同一个位置，这个也叫python的字符串驻留机制</span><br></pre></td></tr></table></figure>

<p>46、以下代码输出什么？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">lis=[&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;,&apos;e&apos;]</span><br><span class="line">print(lis[10:])</span><br><span class="line">答案：[]</span><br></pre></td></tr></table></figure>

<p>47、python哪些类型的数据才能作为字典的key？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">可哈希的类型</span><br></pre></td></tr></table></figure>

<p>48、 以下两段代码的输出一样吗,占用系统资源一样吗, 什么时候要用xrange代替range</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">for  i  in range(1):print(i)</span><br><span class="line">for  i  in xrange(1):print(i)</span><br><span class="line">	输出的结果是一样,占用的资源不一样range比较消耗资源.当数据量比较大的时候使用xrange来代替range比较好</span><br></pre></td></tr></table></figure>

<p>49、有如下代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> copy</span><br><span class="line">a=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,[<span class="number">4</span>,<span class="number">5</span>],<span class="number">6</span>]</span><br><span class="line">b=a</span><br><span class="line">c=copy.copy(a)</span><br><span class="line">d=copy.deepcopy(a)</span><br><span class="line">b.append(<span class="number">10</span>)</span><br><span class="line">c[<span class="number">3</span>].append(<span class="number">11</span>)</span><br><span class="line">d[<span class="number">3</span>].append(<span class="number">12</span>)</span><br><span class="line">求a，b，c，d</span><br><span class="line"></span><br><span class="line">a:[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">11</span>], <span class="number">6</span>, <span class="number">10</span>]</span><br><span class="line">b:[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">11</span>], <span class="number">6</span>, <span class="number">10</span>]</span><br><span class="line">c:[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">11</span>], <span class="number">6</span>]</span><br><span class="line">d:[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, [<span class="number">4</span>, <span class="number">5</span>, <span class="number">12</span>], <span class="number">6</span>]</span><br></pre></td></tr></table></figure>

<p>50对字典d={‘a’:30,’g’:17,’b’:25,’c’:18,’d’:50,’e’:36,’f’:57,’h’:25}按照value字段进行排序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">d=&#123;&apos;a&apos;:30,&apos;g&apos;:17,&apos;b&apos;:25,&apos;c&apos;:18,&apos;d&apos;:50,&apos;e&apos;:36,&apos;f&apos;:57,&apos;h&apos;:25&#125;</span><br><span class="line">dd = sorted(d.items(),key =lambda x:x[1])</span><br><span class="line">print(dd )</span><br></pre></td></tr></table></figure>

<p>51、找出两个列表中相同的元素和不同的元素</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list1=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">8</span>,<span class="number">7</span>,<span class="number">11</span>,<span class="number">10</span>]</span><br><span class="line">list2=[<span class="number">5</span>,<span class="number">15</span>,<span class="number">25</span>,<span class="number">10</span>]</span><br><span class="line">sim=[i <span class="keyword">for</span> i <span class="keyword">in</span> list1 <span class="keyword">if</span> i <span class="keyword">in</span> list2]</span><br><span class="line">diff=[i <span class="keyword">for</span> i <span class="keyword">in</span> list1+list2 <span class="keyword">if</span> i <span class="keyword">not</span> <span class="keyword">in</span> sim]</span><br></pre></td></tr></table></figure>

<p>52、二叉树是非线性结构，栈和队列以及线性表都是线性结构</p>
<p>53、一个栈的输入序列为1,2,3,4,5,则下列序列中不可能是栈的输出序列的是</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">解题思路：栈先进后出</span><br></pre></td></tr></table></figure>

<p>54、下图那些PEP被认为涉及到了代码规范   PEP8</p>
<p>55、python的合法标识符，哪些是python关键字</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int32,saving,ptint,this,self true 这是合法的标识符,True,if yield 这是Python的关键字</span><br></pre></td></tr></table></figure>

<p>56、从0-99这100个数中随机取出10个，要求不能重复</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">import random</span><br><span class="line">lis=random.sample(range(0,100),10)</span><br><span class="line">print(lis)</span><br></pre></td></tr></table></figure>

<p>57、python 判断一个字典中是否有这些key:”AAA”,’BB’,’C’,”DD”,’EEE’(不使用for while)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">key =[<span class="string">"AAA"</span>,<span class="string">'BB'</span>,<span class="string">'C'</span>,<span class="string">"DD"</span>,<span class="string">'EEE'</span>]</span><br><span class="line">dic =&#123;<span class="number">1</span>:<span class="number">2</span>,<span class="number">2</span>:<span class="number">3</span>,<span class="number">3</span>:<span class="number">4</span>&#125;</span><br><span class="line">l = list(dic.keys())</span><br><span class="line"><span class="keyword">if</span> key <span class="keyword">in</span> l:</span><br><span class="line">	print(<span class="string">"含有这些字符"</span>)</span><br><span class="line"><span class="keyword">else</span>：</span><br><span class="line">	print(<span class="string">"不含有这些字符"</span>)</span><br></pre></td></tr></table></figure>

<p>58、有一个列表lis=[‘This’,’is’,’a’,’Man’,’B’,’!’]，对它进行大小写无关的排序</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">lis=[<span class="string">'This'</span>,<span class="string">'is'</span>,<span class="string">'a'</span>,<span class="string">'Man'</span>,<span class="string">'B'</span>,<span class="string">'!'</span>]</span><br><span class="line">lis=sorted(lis,key=str.lower)</span><br><span class="line">print(lis)</span><br></pre></td></tr></table></figure>

<p>59、描述以下字典的items()方法和iteritems()方法有啥不同？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">字典的items方法作用：是可以将字典中的所有项，以列表方式返回。因为字典是无序的，所以用items方法返回字典的所有项，也是没有顺序的。</span><br><span class="line">字典的iteritems方法作用：与items方法相比作用大致相同，只是它的返回值不是列表，而是一个迭代器</span><br></pre></td></tr></table></figure>

<p>60、请列举你所知道的python代码检测工具以及他们之间的区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">pylint --- 源代码分析器，可以分析python代码中的错误</span><br><span class="line">pyflakes --- 检查源文件错误的简单程序，不会检查代码风格。</span><br><span class="line">pep8 --- 检查代码规范的工具</span><br></pre></td></tr></table></figure>

<p>61、介绍一下try except的用法和作用？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">主要用来处理异常</span><br><span class="line">try:</span><br><span class="line">     Normal execution block</span><br><span class="line">except A:</span><br><span class="line">     Exception A handle</span><br><span class="line">except B:</span><br><span class="line">     Exception B handle</span><br><span class="line">except:</span><br><span class="line">     Other exception handle</span><br><span class="line">else:</span><br><span class="line">     if no exception,get here</span><br><span class="line">finally:</span><br><span class="line">     print(&quot;finally&quot;)</span><br></pre></td></tr></table></figure>

<p>62、</p>
<p>63、阅读以下代码，写输出结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">lis = [2,4,5,6,7]</span><br><span class="line">for i in lis:</span><br><span class="line">    if i % 2==0:</span><br><span class="line">        lis.remove(i)</span><br><span class="line">print(lis)</span><br><span class="line">结果：[4, 5, 7]</span><br></pre></td></tr></table></figure>

<p>64、对列表[3,1,-4,-2]按照绝对值排序</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">lis=[3,1,-4,-2]</span><br><span class="line">lis=sorted(lis,key=lambda x:abs(x))</span><br><span class="line">print(lis)</span><br></pre></td></tr></table></figure>

<p>65、填空题</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 表达式 <span class="number">3</span>&lt;<span class="number">4</span>&lt;<span class="number">5</span> 是哪一个表达式的缩写 <span class="comment">#3&lt;4 and 4&lt;5</span></span><br><span class="line"><span class="number">2.</span> 获取Python解释器版本的方法是: <span class="comment">#python -V</span></span><br><span class="line"><span class="number">3.</span> 如果模块是被导入的,__name__的值是__模块的名字__, 如果模块是被直接执行的__name__的值是__main__.</span><br><span class="line"><span class="number">4.</span> Python的内存管理中, 为了追踪内存中的对象, 使用了_引用计数___这一简单技术</span><br><span class="line"><span class="number">5.</span> Python的标准解释器是有C语言实现的, 称为_cpython___, 有Java实现的被称为_jython__.</span><br><span class="line"><span class="number">6.</span> Python中, _del__语句能直接显示的释放内存资源</span><br><span class="line"><span class="number">7.</span> Python的乘方运算符是__**__</span><br></pre></td></tr></table></figure>

<p>66、现有字典mydict和变量onekey, 请写出从mydict中取出onekey值的方法(不止一种写法, 多写加分, 并请叙述不同写法的区别, mydict中是否存在onekey的键值, 不确定)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mydict[&apos;onekey&apos;]</span><br><span class="line">mydict.get(&apos;onekey&apos;) </span><br><span class="line">mydict.setdefault(&apos;onekey&apos;)  #无则添加，有则不变</span><br></pre></td></tr></table></figure>

<p>67、现有一列表alist, 请写出两种去除alist中重复元素的方法, 其中：</p>
<p>​            要求保持原有列表中元素的排列顺序。无需考虑原有列表中元素的排列顺序。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#不保留排序</span></span><br><span class="line">lis=[<span class="number">3</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">list（set(lis)）</span><br><span class="line"><span class="comment">#保留排序</span></span><br><span class="line">lis=[<span class="number">3</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">T=[]</span><br><span class="line">[T.append(i) <span class="keyword">for</span> i <span class="keyword">in</span> lis <span class="keyword">if</span> i <span class="keyword">not</span> <span class="keyword">in</span> T])</span><br><span class="line">print(T)</span><br><span class="line"><span class="comment">#sored</span></span><br><span class="line">T=sorted(set(lis), key=lis.index)</span><br><span class="line">print(T)</span><br></pre></td></tr></table></figure>

<p>68、在什么情况下y!=x-(x-y)会成立？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">x，y是两个不相等的非空集合</span><br><span class="line">x =&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>&#125;</span><br><span class="line">y =&#123;<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>&#125;</span><br></pre></td></tr></table></figure>

<p>69、实现99乘法表（使用两种方法）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">'\n'</span>.join([<span class="string">'\t'</span>.join([<span class="string">'&#123;&#125;*&#123;&#125;=&#123;&#125;'</span>.format(x,y,x*y) <span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">1</span>,y+<span class="number">1</span>)]) <span class="keyword">for</span> y <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">10</span>)]))</span><br><span class="line"><span class="comment">#for循环</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">10</span>):</span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">1</span>,i+<span class="number">1</span>):</span><br><span class="line">        print(<span class="string">'%s*%s=%s'</span>%(i,j,i*j),end=<span class="string">'\t'</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        print()</span><br></pre></td></tr></table></figure>

<p>70、获取list中的元素个数,和向末尾追加元素所用的方法分别是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">获取个数  len()  count</span><br><span class="line">末尾追加  append() insert(<span class="number">-1</span>,<span class="string">'内容)</span></span><br></pre></td></tr></table></figure>

<p>71、判断 dict 中有没有某个 key 用的方法是什么？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">key in dict.keys()</span><br></pre></td></tr></table></figure>

<p>72、填空</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">l=range(<span class="number">100</span>)</span><br><span class="line">python3</span><br><span class="line"><span class="number">1.</span> 如何取第一到第三个元素用的是<span class="comment">#list(l[1:4])</span></span><br><span class="line"><span class="number">2.</span> 如何取倒数第二个元素<span class="comment">#l[-2]</span></span><br><span class="line"><span class="number">3.</span> 如何取后十个 <span class="comment">#list[l[90:]]</span></span><br><span class="line">python2</span><br><span class="line"><span class="number">1.</span> 如何取第一到第三个元素用的是<span class="comment">#l[1:4]</span></span><br><span class="line"><span class="number">2.</span> 如何取倒数第二个元素<span class="comment">#l[-2]</span></span><br><span class="line"><span class="number">3.</span> 如何取后十个 <span class="comment">#l[90:]</span></span><br></pre></td></tr></table></figure>

<p>73、如何判断一个变量是否是字符串</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">isinstance（a,str）</span><br><span class="line">type(a) ==str</span><br></pre></td></tr></table></figure>

<p>74、list和tuple有什么不同？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">list是一个可变的数据结构,能够进行增删改</span><br><span class="line">tuple是一个不可变的数据结构,不能够进行增删改,可以存放一些重要的数据</span><br></pre></td></tr></table></figure>

<p>75、a =dict(zip((“a”,”b”,”c”,”d”,”e”),(1,2,3,4,5)))请问a是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">a = dict(zip((<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>,<span class="string">"e"</span>),(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>)))</span><br><span class="line">print(a)</span><br><span class="line"><span class="comment">#&#123;'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5&#125;</span></span><br></pre></td></tr></table></figure>

<p>76、一行代码生成列表 [1,4,9,16,25,36,49,64,81,100]</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[i*i for i in range(0,11)]</span><br></pre></td></tr></table></figure>

<p>77、只能在循环体和switch语句体内使用break语句</p>
<p>78、读代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>,<span class="number">0</span>,<span class="number">-1</span>):</span><br><span class="line">	print(i)</span><br><span class="line"><span class="comment">#5,4,3,2,1</span></span><br></pre></td></tr></table></figure>

<p>79、写结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">x = <span class="string">"foo"</span></span><br><span class="line">y = <span class="number">2</span></span><br><span class="line">print(x+y)</span><br><span class="line"><span class="comment">#TypeError: cannot concatenate 'str' and 'int' objects</span></span><br></pre></td></tr></table></figure>

<p>80、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">kvps = &#123;<span class="string">"1"</span>:<span class="number">1</span>,<span class="string">"2"</span>:<span class="number">2</span>&#125;</span><br><span class="line">thecopy = kvps</span><br><span class="line">kvps[<span class="string">"1"</span>] = <span class="number">5</span></span><br><span class="line">sum = kvps[<span class="string">"1"</span>] + thecopy[<span class="string">"1"</span>]</span><br><span class="line">print(sum) <span class="comment">#10</span></span><br></pre></td></tr></table></figure>

<p>81、python里如何实现tuple和list的转化</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">tuple([0,1,2,3,4])</span><br><span class="line">list((1,2,3,4,5))</span><br></pre></td></tr></table></figure>

<p>82、type(1+2L*3.14)的结果是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">float <span class="comment">#pyhon2中的</span></span><br></pre></td></tr></table></figure>

<p>83、若k为整型, 下列while循环执行的次数为</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">k = <span class="number">1000</span></span><br><span class="line">a =<span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> k&gt;<span class="number">1</span>:</span><br><span class="line">    a +=<span class="number">1</span></span><br><span class="line">    print(k)</span><br><span class="line">    k=k/<span class="number">2</span></span><br><span class="line"></span><br><span class="line">print(a)</span><br><span class="line"><span class="number">10</span>次</span><br></pre></td></tr></table></figure>

<p>84、以下何者是不合法的布尔表达式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">合法的布尔表达式：</span><br><span class="line">    x <span class="keyword">in</span> range(<span class="number">6</span>)</span><br><span class="line">    e&gt;<span class="number">5</span> <span class="keyword">and</span> <span class="number">4</span>==f</span><br><span class="line">    (x<span class="number">-6</span>)&gt;<span class="number">5</span></span><br><span class="line">不合法的布尔表达式：</span><br><span class="line">	<span class="number">3</span> = a</span><br></pre></td></tr></table></figure>

<p>85、python不支持的数据类型有</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">支持的数据类型：char</span><br><span class="line">不支持的数据类型：int float list</span><br></pre></td></tr></table></figure>

<p>86、如何在python中拷贝一个对象，并说明他们之间的区别？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">使用[:]这样就可以进行拷贝一个对象,这个是浅拷贝</span><br><span class="line">使用copy模块： copy.copy这是浅拷贝。</span><br><span class="line">浅拷贝就是拷贝对象的一层元素.</span><br><span class="line">深拷贝是拷贝对象里的多层元素</span><br></pre></td></tr></table></figure>

<p>87、99（10进制）八进制的表示是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">print</span>（oct(<span class="number">99</span>)）</span><br><span class="line"><span class="comment">#0o143</span></span><br></pre></td></tr></table></figure>

<p>88、下列Python语句正确的是</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">while True:pass</span><br></pre></td></tr></table></figure>

<p>89、list对象alist=</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">alist=[&#123;<span class="string">'name'</span>:<span class="string">'a'</span>,<span class="string">'age'</span>:<span class="number">25</span>&#125;,&#123;<span class="string">'name'</span>:<span class="string">'b'</span>,<span class="string">'age'</span>:<span class="number">30</span>&#125;,&#123;<span class="string">'name'</span>:<span class="string">'c'</span>,<span class="string">'age'</span>:<span class="number">20</span>&#125;]</span><br><span class="line">alist=[&#123;<span class="string">'name'</span>:<span class="string">'a'</span>,<span class="string">'age'</span>:<span class="number">25</span>&#125;,&#123;<span class="string">'name'</span>:<span class="string">'b'</span>,<span class="string">'age'</span>:<span class="number">30</span>&#125;,&#123;<span class="string">'name'</span>:<span class="string">'c'</span>,<span class="string">'age'</span>:<span class="number">20</span>&#125;]</span><br><span class="line">blist=sorted(alist,key=<span class="keyword">lambda</span> x:x[<span class="string">'age'</span>],reverse=<span class="literal">True</span>)</span><br><span class="line">print(blist)</span><br></pre></td></tr></table></figure>

<p>90、关于Python程序的运行方面，有什么手段能提升性能？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">使用多进程，充分利用机器的多核性能</span><br><span class="line">对于性能影响较大的部分代码，可以使用C或C++编写</span><br><span class="line">对于IO阻塞造成的性能影响，可以使用IO多路复用来解决</span><br><span class="line">尽量使用Python的内建函数</span><br><span class="line">尽量使用局部变量</span><br></pre></td></tr></table></figure>

<p>91、python是如何进行内存管理的？python的程序会内存泄漏吗？说说有没有什么方面阻止或者检测内存泄漏？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#引用计数</span></span><br><span class="line">	Python内部使用引用计数，来保持追踪内存中的对象，Python内部记录了对象有多少个引用，就是引用计数，当对象被创建时就创建了一个引用计数，当对象不再需要的时候，这个对象的引用计数为<span class="number">0</span>时，他被垃圾回收。</span><br><span class="line"><span class="comment">#垃圾回收</span></span><br><span class="line">	当内存中有不再使用的部分时，垃圾收集器就会把他们清理掉。他会去检查那些引用计数为<span class="number">0</span>的对象，然后清除其在内存中的空间。当然除了引用计数为<span class="number">0</span>的会被清除，还有一种情况也会被垃圾收集器清掉，当两个对象相互引用时，他们本身其他引用已经为<span class="number">0</span>了。</span><br><span class="line"><span class="comment">#内存池机制</span></span><br><span class="line">	Python提供了对内存的垃圾收集机制，但是他将不用的内存放到内存池而不是反回给操作系统。</span><br><span class="line"><span class="comment">#python的程序会内存泄漏吗？</span></span><br><span class="line">	会发生内存泄漏，在Python程序里，内存泄漏是由于一个长期持有的对象不断的往一个dict或者list对象里添加新的对象, 而又没有即时释放，就会导致这些对象占用的内存越来越多，从而造成内存泄漏。另外，对象的交叉引用也会造成内存无法释放的问题。</span><br><span class="line"><span class="comment">#说说有没有什么方面阻止或者检测内存泄漏？</span></span><br><span class="line">	程序员管理好每个python对象的引用，尽量在不需要使用对象的时候，断开所有引用尽量少通过循环引用组织数据，可以改用weakref做弱引用或者用id之类的句柄访问对象，通过gc模块的接口可以检查出每次垃圾回收有哪些对象不能自动处理，再逐个逐个处理。</span><br></pre></td></tr></table></figure>

<p>92、一个大小为100G的文件etl_log.txt，要读取文件的内容，写出具体过程代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> open(<span class="string">"etl_log.txt"</span>,<span class="string">'r'</span>,encoding=<span class="string">'utf8'</span>) <span class="keyword">as</span> f:</span><br><span class="line">    <span class="keyword">for</span> line <span class="keyword">in</span> f:</span><br><span class="line">        print(line,end=<span class="string">''</span>)</span><br></pre></td></tr></table></figure>

<p>93详细说说tuple,list,dict的用法, 他们的特点</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">tuple 是用小括号()组成,没有增删改,是用于存放一些不修改的数据.里边存放的数据安全系数较高,特点是不可变</span><br><span class="line">list  使用中括号[]组成,有增删改,是用于存放一些数据,经常修改的数据.特点是可变</span><br><span class="line">dict  使用大括号&#123;&#125;组成,有增删改,是用于存放一些键值数据,特点是查找方便.能够准确的查找到</span><br></pre></td></tr></table></figure>

<p>94、已知Alist=[1,2,3,1,2,1,3],如何根据Alist得到[1,2,3]</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">list(set(Alist))</span><br></pre></td></tr></table></figure>

<p>95、已知stra = ‘wqedsfsdrfweedqwedqw’</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>  如何获取最后两个字符</span><br><span class="line">print(s[<span class="number">-2</span>:])</span><br><span class="line"><span class="number">2.</span>  如何获取第二个和第三个字符</span><br><span class="line">print(s[<span class="number">1</span>:<span class="number">3</span>])</span><br></pre></td></tr></table></figure>

<p>96、已知Alist = [“a”,”b”,”‘c’],将Alist转化为’a,b,c’的实现过程</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s = &quot;,&quot;.join(Alist )</span><br></pre></td></tr></table></figure>

<p>97、已知ip=’192.168.0.100’ 代码实现提取各部分并写入列表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ip_list = ip.split(&apos;.&apos;)</span><br></pre></td></tr></table></figure>

<p>98、python代码如何获取命令行参数？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">import sys</span><br><span class="line">sys.argv()</span><br></pre></td></tr></table></figure>

<p>99、写代码tuple1=(‘a’,’b’,’c’,’d’,’e’)，和tuple2=(1,2,3,4,5)得到res={‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’: 5}</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">tuple1=(&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;,&apos;e&apos;)</span><br><span class="line">tuple2=(1,2,3,4,5)</span><br><span class="line">res=dict(zip(tuple1,tuple2))</span><br><span class="line">print(res)</span><br></pre></td></tr></table></figure>

<p>100、字典的写法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;<span class="number">1</span>:<span class="number">0</span>,<span class="number">2</span>:<span class="number">0</span>,<span class="number">3</span>:<span class="number">0</span>&#125;</span><br><span class="line">&#123;<span class="string">'1'</span>:<span class="number">0</span>,<span class="string">'2'</span>:<span class="number">0</span>,<span class="string">'3'</span>:<span class="number">0</span>&#125;</span><br><span class="line">&#123;(<span class="number">1</span>,<span class="number">2</span>):<span class="number">0</span>,(<span class="number">4</span>,<span class="number">3</span>):<span class="number">0</span>&#125;</span><br></pre></td></tr></table></figure>

<p>101、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">kvps =&#123;<span class="string">"1"</span>:<span class="number">1</span>,<span class="string">"2"</span>:<span class="number">2</span>&#125;</span><br><span class="line">thekvp = kvps.copy()</span><br><span class="line">kvps[<span class="string">"1"</span>]=<span class="number">6</span></span><br><span class="line">sum=kvps[<span class="string">"1"</span>]+thekvp[<span class="string">"1"</span>]</span><br><span class="line">print(sum) <span class="number">7</span></span><br></pre></td></tr></table></figure>

<p>102、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">numbers = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">numbers.append([<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>])</span><br><span class="line">print(len(numbers))<span class="number">5</span></span><br></pre></td></tr></table></figure>

<p>103、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">names = [<span class="string">"amair"</span>,<span class="string">"barry"</span>,<span class="string">"charies"</span>,<span class="string">"123"</span>,<span class="string">"dxl"</span>,]</span><br><span class="line"><span class="keyword">if</span> <span class="string">"Amair"</span> <span class="keyword">in</span> names:</span><br><span class="line">    print(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="number">2</span>)</span><br><span class="line"><span class="comment"># 结果：2</span></span><br></pre></td></tr></table></figure>

<p>104、求结果</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(type(1/2)) #&lt;class &apos;float&apos;&gt;</span><br></pre></td></tr></table></figure>

<p>105、以下用来管理Python库管理工具的是</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip</span><br></pre></td></tr></table></figure>

<p>106、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>):</span><br><span class="line">    print(i)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>,<span class="number">6</span>):</span><br><span class="line">    print(i)</span><br><span class="line"><span class="comment">#0 1  4 5</span></span><br></pre></td></tr></table></figure>

<p>107、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line">print(math.floor(<span class="number">5.5</span>)) <span class="comment">#5</span></span><br></pre></td></tr></table></figure>

<p>108、关于python内存理解的说法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">变量不必事先声明</span><br><span class="line">变量无需指定类型</span><br><span class="line">可以使用del释放资源</span><br></pre></td></tr></table></figure>

<p>109python的合法标识</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int32 self name</span><br></pre></td></tr></table></figure>

<p>110下列说法错误的是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">A.  除字典类型外, 所有标准对象均可用于布尔测试 <span class="comment">#字典页可以用于bool测试</span></span><br><span class="line">B.  空字符串的布尔值是<span class="literal">False</span></span><br><span class="line">C.  空列表对象的布尔值是<span class="literal">False</span></span><br><span class="line">D.  值为<span class="number">0</span>的任何数字对象的布尔值是<span class="literal">False</span></span><br><span class="line"><span class="comment">#A</span></span><br></pre></td></tr></table></figure>

<p>111、下列表达是的值为True的是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">A.  <span class="number">5</span>+<span class="number">4j</span> &gt;<span class="number">2</span><span class="number">-3j</span>  <span class="comment">#复数不能比较</span></span><br><span class="line">B.  <span class="number">3</span>&gt;<span class="number">2</span>&gt;<span class="number">2</span></span><br><span class="line">C.  (<span class="number">3</span>,<span class="number">2</span>)&lt;(<span class="string">"a"</span>,<span class="string">"b"</span>) <span class="comment">#int 和str不能比较</span></span><br><span class="line">D.  <span class="string">" abc"</span> &gt; <span class="string">'xyz'</span>  <span class="comment">#false</span></span><br></pre></td></tr></table></figure>

<p>112、关于Python的复数, 下列说法错误的是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">A.  表示复数的语法是 real+imagej</span><br><span class="line">B.  实部和虚部都是浮点数</span><br><span class="line">C.  虚部后缀必须是j, 且必须小写</span><br><span class="line">D.  方法conjugate返回复数的共轭复数</span><br><span class="line"><span class="comment">#c</span></span><br></pre></td></tr></table></figure>

<p>113、下列说法错误的是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">A.  字符应视为长度为<span class="number">1</span>的字符串</span><br><span class="line">B.  字符串以\<span class="number">0</span>标志字符串的结束</span><br><span class="line">C.  既可以用单引号, 也可以用双引号创建字符串</span><br><span class="line">D.  在三引号字符串中可以包含换行回车等特殊字符</span><br><span class="line"><span class="comment">#B</span></span><br></pre></td></tr></table></figure>

<p>114、以下不能创建一个字典的语句是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">A.  dic1 = &#123;&#125;</span><br><span class="line">B.  dic2 = &#123;<span class="number">3</span>:<span class="number">5</span>&#125;</span><br><span class="line">C.  dic3 = &#123;[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]:<span class="string">"usetc"</span>&#125;</span><br><span class="line">D.  dic4 = &#123;(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>):<span class="string">"usetc"</span>&#125;</span><br><span class="line"><span class="comment">#c</span></span><br></pre></td></tr></table></figure>

<p>115、python里面如何拷贝一个对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">list[:]</span><br><span class="line"><span class="keyword">import</span> copy</span><br><span class="line">copy.copy()</span><br><span class="line">赋值是两个对象使用</span><br><span class="line">浅拷贝是不可变数据类型与不可变数据类型都共用一个内容</span><br><span class="line">深拷贝是不可变数据类型共用一个内容，可以数据类型进行完全复制</span><br></pre></td></tr></table></figure>

<p>116、描述在python中的元祖,列表,字典的区别,并且分别写一段定义,添加,删除操作的代码片段。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">元组： 不可变数据类型 （）元素之间逗号分隔   没有添加和删除</span><br><span class="line">列表： 可变数据类型   []表示 元素之间逗号分隔  append、inserd、 remove</span><br><span class="line">字典： &#123;&#125;可变数据类型 元素之间逗号分隔    dic[&quot;&quot;] =&quot;&quot; pop</span><br></pre></td></tr></table></figure>

<p>117、选择结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">name1 = [<span class="string">"Amir"</span>,<span class="string">"Barry"</span>,<span class="string">"chales"</span>,<span class="string">"Dao"</span>]</span><br><span class="line">name2 = name1</span><br><span class="line">name3 = name1[:]</span><br><span class="line">name2[<span class="number">0</span>] =  <span class="string">"asd"</span></span><br><span class="line">name3[<span class="number">1</span>] = <span class="string">"bob"</span></span><br><span class="line">sum =<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> [name1,name2,name3]:</span><br><span class="line">    <span class="keyword">if</span> i[<span class="number">0</span>] == <span class="string">"asd"</span>:</span><br><span class="line">        sum +=<span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> i[<span class="number">1</span>] ==<span class="string">"bob"</span>:</span><br><span class="line">        sum += <span class="number">10</span></span><br><span class="line">print(sum)<span class="comment">#12</span></span><br></pre></td></tr></table></figure>

<p>118、下面程序的输出结果是</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">x = <span class="literal">True</span></span><br><span class="line">y = <span class="literal">False</span></span><br><span class="line">z = <span class="literal">False</span></span><br><span class="line"><span class="keyword">if</span> x <span class="keyword">or</span> y <span class="keyword">and</span> z:</span><br><span class="line">    print(<span class="string">"yes"</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="string">"no"</span>)</span><br><span class="line"><span class="comment">#结果 yes</span></span><br></pre></td></tr></table></figure>

<p>119、1or 2 和  1and 2  输出分别是什么? 为什么</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> <span class="keyword">or</span> <span class="number">2</span> 输出 <span class="number">1</span></span><br><span class="line"><span class="number">1</span> <span class="keyword">and</span> <span class="number">2</span> 输出 <span class="number">2</span></span><br><span class="line"><span class="keyword">or</span> 运算都为真的取<span class="keyword">or</span>前面的</span><br><span class="line"><span class="keyword">and</span> 运算都为真的取<span class="keyword">and</span>后面的</span><br></pre></td></tr></table></figure>

<p>120、1 &lt;(2==2)和1 &lt;2==2的结果分别是什么, 为什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>&lt;(<span class="number">2</span>==<span class="number">2</span>)这样的结果是<span class="literal">False</span> 是因为先算的括号里的内容</span><br><span class="line"><span class="number">1</span>&lt;<span class="number">2</span>==<span class="number">2</span> 这样的结果是<span class="literal">True</span> 是因为这是一个简写 拆开来看是 <span class="number">1</span>&lt;<span class="number">2</span> <span class="keyword">and</span> <span class="number">2</span>==<span class="number">2</span> 所以结果是<span class="literal">True</span></span><br></pre></td></tr></table></figure>

<p>121、如何打乱一个排好序的list对象alist？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">alist =[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line">random.shuffle(alist)</span><br><span class="line">print(alist)</span><br></pre></td></tr></table></figure>

<p>122、如何查找一个字符串中特定的字符?find和index的差异？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">find:找不到返回-1</span><br><span class="line">index:找不到报错</span><br></pre></td></tr></table></figure>

<p>123、把aaabbcccd这种形式的字符串压缩成a3b2c3d1这种形式。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">s= <span class="string">"aaabbcccd"</span></span><br><span class="line">news =<span class="string">""</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> s:</span><br><span class="line">    <span class="keyword">if</span>  i <span class="keyword">not</span> <span class="keyword">in</span> news:</span><br><span class="line">        news +=i +str(s.count(i))</span><br><span class="line">print(news)</span><br><span class="line"><span class="comment">#print("".join(list(set([i + str(s.count(i)) for i in s])))) 乱序不推荐</span></span><br></pre></td></tr></table></figure>

<p>124、Python一个数如果恰好等于它的因子之和，这个数就称为”完数”。例如6=1＋2＋3.编程找出1000以内的所有完数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">wanshu=[]</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">1001</span>):</span><br><span class="line">    s=<span class="number">0</span></span><br><span class="line">    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">1</span>,i//<span class="number">2</span>+<span class="number">1</span>):</span><br><span class="line">        <span class="keyword">if</span> i % j ==<span class="number">0</span>: </span><br><span class="line">            s+=j</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="keyword">if</span> i==s:</span><br><span class="line">            wanshu.append(i)</span><br><span class="line">print(wanshu)</span><br></pre></td></tr></table></figure>

<p>125、输入一个字符串, 输出该字符串中字符的所有组合. </p>
<p>​            例如: 输入字符串”1,2,3”, 则输出为1,2,3,12,13,23,123(组合数, 不考虑顺序, 所以12和21是等价的)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	s = input(<span class="string">"&gt;&gt;&gt;"</span>)  </span><br><span class="line"><span class="number">2.</span>	lst = s.split(<span class="string">","</span>)  </span><br><span class="line"><span class="number">3.</span>	result = []  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>**len(lst)):  </span><br><span class="line"><span class="number">6.</span>	    s = format(i, <span class="string">f"0<span class="subst">&#123;len(lst)&#125;</span>b"</span>)  </span><br><span class="line"><span class="number">7.</span>	    <span class="comment"># print(s)  </span></span><br><span class="line"><span class="number">8.</span>	    ss = <span class="string">""</span>  </span><br><span class="line"><span class="number">9.</span>	    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">0</span>, len(s)):  </span><br><span class="line"><span class="number">10.</span>	        <span class="keyword">if</span> s[j] == <span class="string">'1'</span>:  </span><br><span class="line"><span class="number">11.</span>	            ss += lst[j]  </span><br><span class="line"><span class="number">12.</span>	    <span class="keyword">if</span> ss != <span class="string">""</span>:  </span><br><span class="line"><span class="number">13.</span>	        result.append(ss)  </span><br><span class="line"><span class="number">14.</span>	print(result)</span><br></pre></td></tr></table></figure>

<p>126 执行以下代码后, i和n的值为</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int i=10</span><br><span class="line">int n = i++%5</span><br><span class="line">i，n 11，0</span><br></pre></td></tr></table></figure>

<p>127.执行以下代码段后,x的值为</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int x = <span class="number">10</span></span><br><span class="line">x += x =x-x</span><br><span class="line">x <span class="number">20</span></span><br></pre></td></tr></table></figure>

<p>128、 对于一个非空字符串,判断其是否可以有一个子字符串重复多次组成,字符串只包含小写字母且长度不超过10000</p>
<p>示例1:</p>
<ol>
<li><p>输入”abab”</p>
</li>
<li><p>输出True</p>
</li>
<li><p>样例解释: 输入可由”ab”重复两次组成</p>
</li>
</ol>
<p>示例2:</p>
<ol>
<li><p>输入”abcabcabc”</p>
</li>
<li><p>输出True</p>
</li>
<li><p>样例解释: 输入可由”abc”重复三次组成</p>
</li>
</ol>
<p>示例3:</p>
<ol>
<li>输入”aba”</li>
<li>输出False</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">(s)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    n = len(s)  </span><br><span class="line"><span class="number">3.</span>	    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>, n // <span class="number">2</span> + <span class="number">1</span>):  </span><br><span class="line"><span class="number">4.</span>	        <span class="keyword">if</span> n % i == <span class="number">0</span>:  </span><br><span class="line"><span class="number">5.</span>	            a = s[:i]  </span><br><span class="line"><span class="number">6.</span>	            j = i  </span><br><span class="line"><span class="number">7.</span>	            <span class="keyword">while</span> j &lt; n <span class="keyword">and</span> s[j:j + i] == a:  </span><br><span class="line"><span class="number">8.</span>	                j += i  </span><br><span class="line"><span class="number">9.</span>	            <span class="keyword">if</span> j == n: <span class="keyword">return</span> <span class="literal">True</span>  </span><br><span class="line"><span class="number">10.</span>	  </span><br><span class="line"><span class="number">11.</span>	    <span class="keyword">return</span> <span class="literal">False</span>  </span><br><span class="line"><span class="number">12.</span>	str_input = input(<span class="string">'str&gt;&gt;&gt;'</span>)  </span><br><span class="line"><span class="number">13.</span>	print(foo(str_input))</span><br></pre></td></tr></table></figure>

<h2 id="第二章、函数"><a href="#第二章、函数" class="headerlink" title="第二章、函数"></a>第二章、函数</h2><p>1、通过代码实现如下转换：</p>
<p>​          二进制转换成十进制：v = “0b1111011”<br>         十进制转换成二进制：v = 18<br>         八进制转换成十进制：v = “011”<br>         十进制转换成八进制：v = 30<br>         十六进制转换成十进制：v = “0x12”<br>         十进制转换成十六进制：v = 87</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	二进制转换成十进制：v=“<span class="number">0b1111011</span>”  </span><br><span class="line"><span class="number">2.</span>	  </span><br><span class="line"><span class="number">3.</span>	v=<span class="string">"1111011"</span>  </span><br><span class="line"><span class="number">4.</span>	print(int(v,<span class="number">2</span>))  </span><br><span class="line"><span class="number">5.</span>	结果:<span class="number">123</span>  </span><br><span class="line"><span class="number">6.</span>	  </span><br><span class="line"><span class="number">7.</span>	十进制转换成二进制：V=<span class="number">18</span>  </span><br><span class="line"><span class="number">8.</span>	v=<span class="number">18</span>  </span><br><span class="line"><span class="number">9.</span>	print(bin(v))  </span><br><span class="line"><span class="number">10.</span>	结果:  <span class="number">0b10010</span>  </span><br><span class="line"><span class="number">11.</span>	  </span><br><span class="line"><span class="number">12.</span>	八进制转换成十进制：v=“<span class="number">011</span>”  </span><br><span class="line"><span class="number">13.</span>	  </span><br><span class="line"><span class="number">14.</span>	print(int(<span class="string">'011'</span>,<span class="number">8</span>))  </span><br><span class="line"><span class="number">15.</span>	结果: <span class="number">9</span>  </span><br><span class="line"><span class="number">16.</span>	  </span><br><span class="line"><span class="number">17.</span>	十进制转换成八进制：v=<span class="number">30</span>  </span><br><span class="line"><span class="number">18.</span>	  </span><br><span class="line"><span class="number">19.</span>	print(oct(<span class="number">30</span>))  </span><br><span class="line"><span class="number">20.</span>	结果: <span class="number">0o36</span>  </span><br><span class="line"><span class="number">21.</span>	  </span><br><span class="line"><span class="number">22.</span>	十六进制转换成十进制：V=“<span class="number">0x12</span>”  </span><br><span class="line"><span class="number">23.</span>	print(int(<span class="string">'0x12'</span>,<span class="number">16</span>))  </span><br><span class="line"><span class="number">24.</span>	结果:  <span class="number">18</span>  </span><br><span class="line"><span class="number">25.</span>	  </span><br><span class="line"><span class="number">26.</span>	十进制转换成十六进制：v=<span class="number">87</span>  </span><br><span class="line"><span class="number">27.</span>	print(hex(<span class="number">87</span>))  </span><br><span class="line"><span class="number">28.</span>	结果:  </span><br><span class="line"><span class="number">29.</span>	<span class="number">0x57</span>  </span><br><span class="line">总结：</span><br><span class="line">转换成<span class="number">10</span>进制 int（<span class="string">"待转数字"</span>，<span class="string">"原数字位数"</span>）</span><br><span class="line"><span class="number">10</span>转换成<span class="number">2</span>进制 bin（<span class="string">"待转数字"</span>）</span><br><span class="line"><span class="number">10</span>转换成进<span class="number">8</span>制 oct（<span class="string">"待转数字"</span>）</span><br><span class="line"><span class="number">10</span>转换成<span class="number">16</span>进制 hex（<span class="string">"待转数字"</span>）</span><br></pre></td></tr></table></figure>

<p>2、Python递归的最大层数？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">答：官方表明,递归的最大层数是1000,</span><br><span class="line">	我们本地测试是998或997</span><br></pre></td></tr></table></figure>

<p>3、列举常见的内置函数？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">map sorted fliter zip round divmod max min sum <span class="keyword">print</span></span><br><span class="line">input</span><br></pre></td></tr></table></figure>

<p>4、filter、map、reduce的作用？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">答: filter 的作用是过滤条件</span><br><span class="line">	map    的作用是将内容里的元素逐个做处理</span><br><span class="line">	reduce 是用于做累计算</span><br></pre></td></tr></table></figure>

<p>5、什么是闭包？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1、一个嵌套函数</span><br><span class="line">2、函数内部引用非全局变量</span><br></pre></td></tr></table></figure>

<p>6、简述生成器、迭代器、装饰器以及应用场景</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">生成器(迭代器)应用场景:</span><br><span class="line">	读取(获取)较大的文件和内容的时候使用,防止内存溢出</span><br><span class="line">装饰器应用场景:</span><br><span class="line">	自定义用户认证的时候</span><br><span class="line">	flask框架路由系统</span><br><span class="line">	Django认证</span><br><span class="line">	类的私有变量的封装的时候用到</span><br></pre></td></tr></table></figure>

<p>7、使用生成器编写fib函数, 函数声明为fib(max), 输入一个参数max值, 使得该函数可以这样调用。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">0</span>,<span class="number">100</span>):  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">print</span> fib(<span class="number">1000</span>) </span><br><span class="line">并产生如下结果(斐波那契数列),<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">8</span>,<span class="number">13</span>,<span class="number">21.</span>..</span><br><span class="line"></span><br><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">fib</span><span class="params">(max)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    a = <span class="number">0</span>  </span><br><span class="line"><span class="number">3.</span>	    b = <span class="number">1</span>  </span><br><span class="line"><span class="number">4.</span>	    <span class="keyword">while</span> b&lt;max:  </span><br><span class="line"><span class="number">5.</span>	        <span class="keyword">yield</span> b  </span><br><span class="line"><span class="number">6.</span>	        b,a = a+b,b  </span><br><span class="line"><span class="number">7.</span>	  </span><br><span class="line"><span class="number">8.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> fib(<span class="number">1000</span>):  </span><br><span class="line"><span class="number">9.</span>	    print(i)</span><br></pre></td></tr></table></figure>

<p>8、一行代码, 通过filter和lambda函数输出以下列表索引为基数对应的元素。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">问题：</span><br><span class="line"><span class="comment">#list_a=[12,213,22,2,2,2,22,2,2,32]</span></span><br><span class="line">答案：</span><br><span class="line">new_list = [x[<span class="number">1</span>]  <span class="keyword">for</span> x <span class="keyword">in</span> filter(<span class="keyword">lambda</span> x:x[<span class="number">0</span>]%<span class="number">2</span>==<span class="number">1</span>,enumerate(list_as))]  </span><br><span class="line">print(new_list)</span><br></pre></td></tr></table></figure>

<p>9、写一个base62encode函数，62进制</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#0123456789AB..Zab..z(10个数字+26个大写字母+26个小写字母)。</span></span><br><span class="line">	base62encode(<span class="number">1</span>)=<span class="number">1</span></span><br><span class="line">	base62encode(<span class="number">61</span>) = z </span><br><span class="line">	base62encode(<span class="number">62</span>)=<span class="number">10</span></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span>	<span class="keyword">import</span> string  </span><br><span class="line"><span class="number">2.</span>	s_lower = string.ascii_lowercase  <span class="comment"># 小写字母  </span></span><br><span class="line"><span class="number">3.</span>	s_upper = string.ascii_uppercase  <span class="comment"># 大写字母  </span></span><br><span class="line"><span class="number">4.</span>	s_num = string.digits  <span class="comment"># 0-9  </span></span><br><span class="line"><span class="number">5.</span>	s = s_num + s_upper + s_lower  </span><br><span class="line"><span class="number">6.</span>	  </span><br><span class="line"><span class="number">7.</span>	  </span><br><span class="line"><span class="number">8.</span>	<span class="function"><span class="keyword">def</span> <span class="title">base62encode</span><span class="params">(num)</span>:</span>  </span><br><span class="line"><span class="number">9.</span>	    new_s = <span class="string">""</span>  </span><br><span class="line"><span class="number">10.</span>	    <span class="keyword">while</span> <span class="literal">True</span>:  </span><br><span class="line"><span class="number">11.</span>	        new_s = s[num % <span class="number">62</span>] + new_s  </span><br><span class="line"><span class="number">12.</span>	        <span class="keyword">if</span> num // <span class="number">62</span> == <span class="number">0</span>:  </span><br><span class="line"><span class="number">13.</span>	            <span class="keyword">break</span>  </span><br><span class="line"><span class="number">14.</span>	        num = num // <span class="number">62</span>  </span><br><span class="line"><span class="number">15.</span>	    <span class="keyword">return</span> new_s  </span><br><span class="line"><span class="number">16.</span>	  </span><br><span class="line"><span class="number">17.</span>	  </span><br><span class="line"><span class="number">18.</span>	print(base62encode(<span class="number">36659</span>))</span><br></pre></td></tr></table></figure>

<p>10、请实现一个装饰器, 限制该函数被调用的频率, 如10秒一次</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="keyword">import</span> time  </span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(fn)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	    t = <span class="number">0</span>  </span><br><span class="line"><span class="number">4.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">inner</span><span class="params">(*args, **kwargs)</span>:</span>  </span><br><span class="line"><span class="number">5.</span>	        <span class="keyword">nonlocal</span> t  </span><br><span class="line"><span class="number">6.</span>	        tt = time.time()  </span><br><span class="line"><span class="number">7.</span>	        <span class="keyword">if</span> tt - t &gt;= <span class="number">10</span>:  </span><br><span class="line"><span class="number">8.</span>	            t = time.time()  </span><br><span class="line"><span class="number">9.</span>	            <span class="keyword">return</span> fn(*args, **kwargs)  </span><br><span class="line"><span class="number">10.</span>	        <span class="keyword">else</span>:  </span><br><span class="line"><span class="number">11.</span>	            print(<span class="string">"限制访问"</span>)  </span><br><span class="line"><span class="number">12.</span>	    <span class="keyword">return</span> inner  </span><br><span class="line"><span class="number">13.</span>	 </span><br><span class="line"><span class="number">14.</span>	@wrapper  </span><br><span class="line"><span class="number">15.</span>	<span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">16.</span>	    print(<span class="string">'is foo'</span>)  </span><br><span class="line"><span class="number">17.</span>	  </span><br><span class="line"><span class="number">18.</span>	foo()  </span><br><span class="line"><span class="number">19.</span>	time.sleep(<span class="number">3</span>)  </span><br><span class="line"><span class="number">20.</span>	foo()  </span><br><span class="line"><span class="number">21.</span>	time.sleep(<span class="number">5</span>)  </span><br><span class="line"><span class="number">22.</span>	foo()  </span><br><span class="line"><span class="number">23.</span>	foo()  </span><br><span class="line"><span class="number">24.</span>	foo()  </span><br><span class="line"><span class="number">25.</span>	foo()</span><br></pre></td></tr></table></figure>

<p>11、请实现一个装饰器, 通过一次调用使函数重复执行5次</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="keyword">from</span> datetime <span class="keyword">import</span> datetime  </span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">decorator</span><span class="params">(func)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">decorated_func</span><span class="params">(*args,**kwargs)</span>:</span>  </span><br><span class="line"><span class="number">4.</span>	        <span class="comment"># 函数的名称作为key  </span></span><br><span class="line"><span class="number">5.</span>	     </span><br><span class="line"><span class="number">6.</span>	        <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>):  </span><br><span class="line"><span class="number">7.</span>	            func(key)  </span><br><span class="line"><span class="number">8.</span>	    <span class="keyword">return</span> decorated_func  </span><br><span class="line"><span class="number">9.</span>	 </span><br><span class="line"><span class="number">10.</span>	@decorator  </span><br><span class="line"><span class="number">11.</span>	<span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">(x)</span>:</span>  </span><br><span class="line"><span class="number">12.</span>	    print(<span class="string">'执行%s函数,时间:%s'</span>%(x,datetime.now()))  </span><br><span class="line"><span class="number">13.</span>	  </span><br><span class="line"><span class="number">14.</span>	func(<span class="number">5</span>)</span><br></pre></td></tr></table></figure>

<p>12、python一行print出1~100偶数的列表, (列表推导式, filter均可)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="comment"># 方式一:  </span></span><br><span class="line"><span class="number">2.</span>	print([i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">101</span>) <span class="keyword">if</span> i%<span class="number">2</span> == <span class="number">0</span>])  </span><br><span class="line"><span class="number">3.</span>	<span class="comment"># 方式二:  </span></span><br><span class="line"><span class="number">4.</span>	print(list(filter(<span class="keyword">lambda</span> x:x%<span class="number">2</span>==<span class="number">0</span>,range(<span class="number">1</span>,<span class="number">101</span>))))</span><br></pre></td></tr></table></figure>

<p>13、解释生成器与函数的不同, 并实现和简单使用generator.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	生成器就是函数体中有 <span class="keyword">yield</span> 关键字,函数没有  </span><br><span class="line"><span class="number">2.</span>	<span class="comment"># 实现生成器:  </span></span><br><span class="line"><span class="number">3.</span>	<span class="function"><span class="keyword">def</span> <span class="title">g</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">4.</span>	    print(<span class="string">'one'</span>)  </span><br><span class="line"><span class="number">5.</span>	    <span class="keyword">yield</span> <span class="string">'two'</span>  </span><br><span class="line"><span class="number">6.</span>	  </span><br><span class="line"><span class="number">7.</span>	<span class="comment"># 使用生成器:  </span></span><br><span class="line"><span class="number">8.</span>	g1 = g()  <span class="comment"># 实例一个生成器对象  </span></span><br><span class="line"><span class="number">9.</span>	print(g1.__next__())  </span><br><span class="line"><span class="number">10.</span>	结果:  </span><br><span class="line"><span class="number">11.</span>	one  </span><br><span class="line"><span class="number">12.</span>	two</span><br></pre></td></tr></table></figure>

<p>14、列表推导式和生成器表达式 [i % 2 for i in range(10)] 和 (i % 2 for i in range(10)) 输出结果分别是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	print([i %<span class="number">2</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>)])  </span><br><span class="line"><span class="number">2.</span>	<span class="comment"># 结果: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]  </span></span><br><span class="line"><span class="number">3.</span>	  </span><br><span class="line"><span class="number">4.</span>	print((i %<span class="number">2</span> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>)))  </span><br><span class="line"><span class="number">5.</span>	<span class="comment"># 结果: &lt;generator object &lt;genexpr&gt; at 0x000001CF725EDA98&gt;</span></span><br></pre></td></tr></table></figure>

<p>15、map(str,[1,2,3,4,5,6,7,8,9]) 输出是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	python3写法:  </span><br><span class="line"><span class="number">2.</span>	    print(map(str,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>]))  </span><br><span class="line"><span class="number">3.</span>	    结果: &lt;map object at <span class="number">0x00000166A41D9358</span>&gt;  </span><br><span class="line"><span class="number">4.</span>	python2写法:  </span><br><span class="line"><span class="number">5.</span>	    print(map(str,[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>]))  </span><br><span class="line"><span class="number">6.</span>	    结果: [<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>, <span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>]</span><br></pre></td></tr></table></figure>

<p>16、python中定义函数时如何书写可变参数和关键字参数?</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">(*args,k=<span class="number">5</span>,**kwargs)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    print(*args,k,kwargs)  </span><br><span class="line"><span class="number">3.</span>	  </span><br><span class="line"><span class="number">4.</span>	foo(<span class="string">'aa'</span>,<span class="string">'sd'</span>,<span class="string">'abc'</span>,a=<span class="number">1</span>,b=<span class="number">2</span>)  </span><br><span class="line"><span class="number">5.</span>	结果: aa sd abc <span class="number">5</span> &#123;<span class="string">'a'</span>: <span class="number">1</span>, <span class="string">'b'</span>: <span class="number">2</span>&#125;  </span><br><span class="line"><span class="number">6.</span>	可变位置参数需要在定义的时候在变量前面加一个*号,可变关键字参数前面加一个**号</span><br></pre></td></tr></table></figure>

<p>17、Python3.5中enumerate的意思是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="comment"># 枚举,就是可以自己定义一个序号  </span></span><br><span class="line"><span class="number">2.</span>	s = <span class="string">'你好'</span>  </span><br><span class="line"><span class="number">3.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> enumerate(s):  </span><br><span class="line"><span class="number">4.</span>	    print(i)  </span><br><span class="line"><span class="number">5.</span>	<span class="comment"># 结果:  </span></span><br><span class="line"><span class="number">6.</span>	(<span class="number">0</span>, <span class="string">'你'</span>)  </span><br><span class="line"><span class="number">7.</span>	(<span class="number">1</span>, <span class="string">'好'</span>)</span><br></pre></td></tr></table></figure>

<p>18、说说Python中的装饰器,迭代器的用法:描述下dict的item方法与iteritems方法的不同</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">装饰器是在不修改原函数的基础上添加一个功能</span><br><span class="line">迭代器是读取大文件时使用的</span><br><span class="line">python3中没有iteritems</span><br><span class="line">items和iteritems大致相同，只是items返回的是一个列表，iteritems返回的是一个迭代器。</span><br></pre></td></tr></table></figure>

<p>19、是否使用过functools中的函数？其作用是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">reduce</span><br><span class="line">作用是进行累计算</span><br><span class="line">wraps</span><br><span class="line">作用是消除装饰带来的副作用(我们在调用被装饰的函数其实已经不是它本身,我们使用wraps就能消除这个问题)</span><br></pre></td></tr></table></figure>

<p>20、如何判断一个值是函数还是方法？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line">a =A()</span><br><span class="line"><span class="keyword">from</span> types <span class="keyword">import</span> FunctionType,MethodType</span><br><span class="line">print(isinstance(func,FunctionType)) <span class="comment">#True</span></span><br><span class="line">print(isinstance(a.func,MethodType)) <span class="comment">#True</span></span><br></pre></td></tr></table></figure>

<p>21、请编写一个函数实现将IP地址转换成一个整数。</p>
<p>如10.3.9.12 转换规则为：<br>        10            00001010<br>         3             00000011<br>         9             00001001<br>        12            00001100<br>再将以上二进制拼接起来计算十进制结果：00001010<br>00000011 00001001 00001100 = ？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">ip</span><span class="params">()</span>:</span></span><br><span class="line">    ip = <span class="string">'10.3.9.12'</span></span><br><span class="line">    ip_list = ip.split(<span class="string">"."</span>)</span><br><span class="line">    num = <span class="string">""</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> ip_list:</span><br><span class="line">        i = int(i)</span><br><span class="line">        num = num + format(i,<span class="string">"08b"</span>)</span><br><span class="line">    count = int(num,<span class="number">2</span>)</span><br><span class="line">    print(count)</span><br><span class="line">ip()</span><br></pre></td></tr></table></figure>

<p>22、lambda表达式格式以及应用场景</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># # min:</span></span><br><span class="line"><span class="comment"># li = [-1,2,-3,4]</span></span><br><span class="line"><span class="comment"># l_min = min(li,key=lambda x:x)</span></span><br><span class="line"><span class="comment"># print(l_min)</span></span><br><span class="line"><span class="comment"># max</span></span><br><span class="line"><span class="comment"># li = [-1,2,-3,4]</span></span><br><span class="line"><span class="comment"># l_max = max(li,key=lambda x:x)</span></span><br><span class="line"><span class="comment"># print(l_max)</span></span><br><span class="line"><span class="comment">#fliter:</span></span><br><span class="line"><span class="comment"># li = [-1,2,-3,4]</span></span><br><span class="line"><span class="comment"># l_fliter = filter(lambda x:x&gt;2,li)</span></span><br><span class="line"><span class="comment"># print(list(l_fliter))#[4]</span></span><br><span class="line"><span class="comment"># map 映射</span></span><br><span class="line"><span class="comment"># li = [-1,2,-3,4]</span></span><br><span class="line"><span class="comment"># l_map = map(lambda x:x*5,li)</span></span><br><span class="line"><span class="comment"># print(list(l_map))#[4]</span></span><br><span class="line"><span class="comment">#sorted排序</span></span><br><span class="line"><span class="comment"># dic = &#123;"a":1,"b":2&#125;</span></span><br><span class="line"><span class="comment"># s = sorted(dic.items(),key=lambda x:x[1],reverse=True)</span></span><br><span class="line"><span class="comment"># print(s)</span></span><br><span class="line"><span class="comment">#reduce</span></span><br><span class="line"><span class="comment">#python2</span></span><br><span class="line"><span class="comment"># l = [1,2,3,4,5,6,7,8]</span></span><br><span class="line"><span class="comment"># s = reduce(lambda x,y:x+y,l)</span></span><br><span class="line"><span class="comment"># print(s)</span></span><br><span class="line"><span class="comment">#python3</span></span><br><span class="line"><span class="keyword">from</span> functools <span class="keyword">import</span> reduce</span><br><span class="line">l = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>]</span><br><span class="line">s = reduce(<span class="keyword">lambda</span> x,y:x+y,l)</span><br><span class="line">print(s)</span><br></pre></td></tr></table></figure>

<p>23、pass的作用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">用来占位，以后要完成的代码</span><br></pre></td></tr></table></figure>

<p>24、<em>arg和*</em>kwarg作用?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">答：*args：（表示的就是将实参中按照位置传值，多出来的值都给args，且以元组的方式呈现）</span><br><span class="line">  	**kwargs：（表示的就是实参中按照关键字传值把多余的传值以字典的方式呈现）</span><br></pre></td></tr></table></figure>

<p>25、如何在函数中设置一个全局变量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">global</span> a  </span><br><span class="line"><span class="number">3.</span>	    a = <span class="number">10</span>  </span><br><span class="line"><span class="number">4.</span>	func()  </span><br><span class="line"><span class="number">5.</span>	print(a)</span><br></pre></td></tr></table></figure>

<p>26 写出打印结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="comment"># 例 1  </span></span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">(a,b=[])</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	  b.append(a)  </span><br><span class="line"><span class="number">4.</span>	    print(b)  </span><br><span class="line"><span class="number">5.</span>	func(<span class="number">1</span>)                                       [<span class="number">1</span>]</span><br><span class="line"><span class="number">6.</span>	func(<span class="number">1</span>)                                       [<span class="number">1</span>,<span class="number">1</span>]</span><br><span class="line"><span class="number">7.</span>	func(<span class="number">1</span>)                                       [<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>]</span><br><span class="line"><span class="number">8.</span>	func(<span class="number">1</span>)                                       [<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">1</span>]</span><br><span class="line"><span class="number">9.</span>	      </span><br><span class="line"><span class="number">10.</span>	<span class="comment"># 例 2  </span></span><br><span class="line"><span class="number">11.</span>	<span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">(a,b=&#123;&#125;)</span>:</span>  </span><br><span class="line"><span class="number">12.</span>	  b[a] = <span class="string">'v'</span>  </span><br><span class="line"><span class="number">13.</span>	  print(b)  </span><br><span class="line"><span class="number">14.</span>	func(<span class="number">1</span>)                                       &#123;<span class="number">1</span>:’v’&#125;</span><br><span class="line"><span class="number">15.</span>	func(<span class="number">2</span>)                                       &#123;<span class="number">1</span>:’v’,<span class="number">2</span>:’v’&#125;</span><br></pre></td></tr></table></figure>

<p>27求结果 lambda</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">num</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	  <span class="keyword">return</span> [<span class="keyword">lambda</span> x:i*x <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>)]  </span><br><span class="line"><span class="number">3.</span>	print([m(<span class="number">2</span>) <span class="keyword">for</span> m <span class="keyword">in</span> num()]) </span><br><span class="line">[<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>]</span><br></pre></td></tr></table></figure>

<p>28 简述 yield和yield from关键字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">yield</span>是将内容一次性返回</span><br><span class="line"><span class="keyword">yield</span> <span class="keyword">from</span> 是将可迭代对象中的元素逐个返回</span><br></pre></td></tr></table></figure>

<p>29、有processFunc变量 ,初始化为processFunc = collapse and (lambda s:” “.join(s.split())) or (lambda s:s) 调用上下文如下</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	collapse = <span class="literal">True</span>  </span><br><span class="line"><span class="number">2.</span>	processFunc = collapse <span class="keyword">and</span> (<span class="keyword">lambda</span> s:<span class="string">" "</span>.join(s.split())) <span class="keyword">or</span> (<span class="keyword">lambda</span> s:s)  </span><br><span class="line"><span class="number">3.</span>	<span class="keyword">print</span> processFunc(<span class="string">"i\tam\ntest\tobject !"</span>)  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	collapse = <span class="literal">False</span>  </span><br><span class="line"><span class="number">6.</span>	processFunc = collapse <span class="keyword">and</span> (<span class="keyword">lambda</span> s:<span class="string">" "</span>.join(s.split())) <span class="keyword">or</span> (<span class="keyword">lambda</span> s:s)  </span><br><span class="line"><span class="number">7.</span>	<span class="keyword">print</span> processFunc(<span class="string">"i\tam\ntest\tobject !"</span>) </span><br><span class="line"></span><br><span class="line"><span class="comment">#什么都不写可以按/或\t分隔</span></span><br><span class="line"><span class="comment">#结果：</span></span><br><span class="line">i am test object </span><br><span class="line"></span><br><span class="line">i am </span><br><span class="line">test object</span><br></pre></td></tr></table></figure>

<p>30、请给出下面代码的输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	a = <span class="number">1</span>  </span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">fun</span><span class="params">(a)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	    a = <span class="number">2</span>  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	fun(a)  </span><br><span class="line"><span class="number">6.</span>	<span class="keyword">print</span> a  <span class="comment">#1</span></span><br><span class="line"><span class="number">7.</span>	  </span><br><span class="line"><span class="number">8.</span>	a = []  </span><br><span class="line"><span class="number">9.</span>	<span class="function"><span class="keyword">def</span> <span class="title">fun</span><span class="params">(a)</span>:</span>  </span><br><span class="line"><span class="number">10.</span>	    a.append(<span class="number">1</span>)  </span><br><span class="line"><span class="number">11.</span>	  </span><br><span class="line"><span class="number">12.</span>	fun(a)  <span class="comment">#[1]</span></span><br><span class="line"><span class="number">13.</span>	<span class="keyword">print</span> a  <span class="comment">#[1]</span></span><br></pre></td></tr></table></figure>

<p>31、全局变量和局部变量的区别,如何给function里面的一个全局变量赋值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1.	全局变量是在任何位置都可以访问,局部变量只能是在局部的这个空间中能够访问  </span><br><span class="line">2.	def func():  </span><br><span class="line">3.	    global a  </span><br><span class="line">4.	    a = 10  </span><br><span class="line">5.	func()  </span><br><span class="line">6.	print(a)  # 这样能够访问到就说明在func函数中成功的给全局创建了一个变量</span><br></pre></td></tr></table></figure>

<p>32、什么是lambda函数, 下面这段代码的输出是什么</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	nums = range(<span class="number">2</span>,<span class="number">20</span>)  </span><br><span class="line"><span class="number">2.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> nums:  </span><br><span class="line"><span class="number">3.</span>	    nums = filter(<span class="keyword">lambda</span> x:x==i <span class="keyword">or</span> x % i, nums)  </span><br><span class="line"><span class="number">4.</span>	nums</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">1.</span>	<span class="keyword">lambda</span>函数是一个匿名函数 也是一行代码的函数  </span><br><span class="line"><span class="number">2.</span>	<span class="comment"># python3  </span></span><br><span class="line"><span class="number">3.</span>	nums=range(<span class="number">2</span>,<span class="number">20</span>)  </span><br><span class="line"><span class="number">4.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> nums:  </span><br><span class="line"><span class="number">5.</span>	    nums= filter(<span class="keyword">lambda</span> x:x==i <span class="keyword">or</span> x%i,nums)  </span><br><span class="line"><span class="number">6.</span>	print(nums)  </span><br><span class="line"><span class="number">7.</span>	<span class="comment"># 结果:  </span></span><br><span class="line"><span class="number">8.</span>	    &lt;filter object at <span class="number">0x000001925F1B6780</span>&gt;  </span><br><span class="line"><span class="number">9.</span>	      </span><br><span class="line"><span class="number">10.</span>	<span class="comment"># python2  </span></span><br><span class="line"><span class="number">11.</span>	nums=range(<span class="number">2</span>,<span class="number">20</span>)  </span><br><span class="line"><span class="number">12.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> nums:  </span><br><span class="line"><span class="number">13.</span>	    nums= filter(<span class="keyword">lambda</span> x:x==i <span class="keyword">or</span> x%i,nums)  </span><br><span class="line"><span class="number">14.</span>	print(nums)  </span><br><span class="line"><span class="number">15.</span>	<span class="comment"># 结果:  </span></span><br><span class="line"><span class="number">16.</span>	[<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">17</span>, <span class="number">19</span>]</span><br></pre></td></tr></table></figure>

<p>33、指出下面程序存在的问题</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">Lastllindextem</span><span class="params">(src, index)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="string">'''''请返回传入src使用空格或者"\"切分后的倒数第index个子串'''</span>  </span><br><span class="line"><span class="number">3.</span>	    <span class="keyword">return</span> src.split(<span class="string">"\")[-index]</span></span><br><span class="line"><span class="string">#没有进行转译</span></span><br></pre></td></tr></table></figure>

<p>34、有一个数组[3,4,1,2,5,6,6,5,4,3,3]，请写一个函数, 找出该数组中没有重复的数的总和. (上面数据的么有重复的总和为1+2=3)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> lis:</span><br><span class="line">        <span class="keyword">if</span> lis.count(i) == <span class="number">1</span>:</span><br><span class="line">            li.append(i)</span><br><span class="line">    print(sum(li))</span><br><span class="line">foo()</span><br></pre></td></tr></table></figure>

<p>35、求结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]  </span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">bar</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	    arr+=[<span class="number">5</span>]  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	bar()  </span><br><span class="line"><span class="number">6.</span>	<span class="keyword">print</span> arr </span><br><span class="line"><span class="comment"># [1,2,3,5]</span></span><br></pre></td></tr></table></figure>

<p>36、请写一个函数, 计算出如下几个字母代表的数字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">AB-CD=EF</span><br><span class="line">EF+GH = PPP</span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="number">1.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>,<span class="number">100</span>):  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">for</span> j <span class="keyword">in</span> range(<span class="number">10</span>, <span class="number">100</span>):  </span><br><span class="line"><span class="number">3.</span>	        <span class="keyword">for</span> k <span class="keyword">in</span> range(<span class="number">10</span>, <span class="number">100</span>):  </span><br><span class="line"><span class="number">4.</span>	            <span class="keyword">if</span> i != j <span class="keyword">and</span> i != k <span class="keyword">and</span> k != j <span class="keyword">and</span> i-j+k == <span class="number">111</span> <span class="keyword">and</span> i - j &gt;= <span class="number">10</span>:  </span><br><span class="line"><span class="number">5.</span>	                E = str(i - j)[<span class="number">0</span>]  </span><br><span class="line"><span class="number">6.</span>	                F = str(i - j)[<span class="number">1</span>]  </span><br><span class="line"><span class="number">7.</span>	                A = str(i)[<span class="number">0</span>]  </span><br><span class="line"><span class="number">8.</span>	                B = str(i)[<span class="number">1</span>]  </span><br><span class="line"><span class="number">9.</span>	                C = str(j)[<span class="number">0</span>]  </span><br><span class="line"><span class="number">10.</span>	                D = str(j)[<span class="number">1</span>]  </span><br><span class="line"><span class="number">11.</span>	                G = str(k)[<span class="number">0</span>]  </span><br><span class="line"><span class="number">12.</span>	                H = str(k)[<span class="number">1</span>]  </span><br><span class="line"><span class="number">13.</span>	                  </span><br><span class="line"><span class="number">14.</span>	                s = &#123;A,B,C,D,E,F,G,H&#125;  </span><br><span class="line"><span class="number">15.</span>	                <span class="keyword">if</span> len(s) == <span class="number">8</span> <span class="keyword">and</span> <span class="string">"1"</span> <span class="keyword">not</span> <span class="keyword">in</span> s:  </span><br><span class="line"><span class="number">16.</span>	                    print(A,B,C,D,E,F,G,H)</span><br></pre></td></tr></table></figure>

<p>37、请给出下面代码片段的输出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	 <span class="function"><span class="keyword">def</span> <span class="title">say_hi</span><span class="params">(func)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(*args,**kwargs)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	        print(<span class="string">"HI"</span>)  </span><br><span class="line"><span class="number">4.</span>	        ret = func(*args,**kwargs)  </span><br><span class="line"><span class="number">5.</span>	        print(<span class="string">"BYE"</span>)  </span><br><span class="line"><span class="number">6.</span>	        <span class="keyword">return</span> ret  </span><br><span class="line"><span class="number">7.</span>	    <span class="keyword">return</span> wrapper  </span><br><span class="line"><span class="number">8.</span>	  </span><br><span class="line"><span class="number">9.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">say_yo</span><span class="params">(func)</span>:</span>  </span><br><span class="line"><span class="number">10.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">wrapper</span><span class="params">(*args,**kwargs)</span>:</span>  </span><br><span class="line"><span class="number">11.</span>	        print(<span class="string">"YO"</span>)  </span><br><span class="line"><span class="number">12.</span>	        <span class="keyword">return</span> func(*args,**kwargs)  </span><br><span class="line"><span class="number">13.</span>	    <span class="keyword">return</span> wrapper  </span><br><span class="line"><span class="number">14.</span>	 </span><br><span class="line"><span class="number">15.</span>	@say_hi  </span><br><span class="line"><span class="number">16.</span>	@say_yo  </span><br><span class="line"><span class="number">17.</span>	<span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">18.</span>	    print(<span class="string">"ROCK &amp; ROLL"</span>)  </span><br><span class="line"><span class="number">19.</span>	  </span><br><span class="line"><span class="number">20.</span>	func() </span><br><span class="line">    HI</span><br><span class="line">    Yo</span><br><span class="line">    ROCK&amp;ROLL</span><br><span class="line">    BYE</span><br></pre></td></tr></table></figure>

<p>38、请简述标准库中functools.wraps的作用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1、python装饰器装饰函数的时候，被装饰的的函数的函数名会发生变化</span><br><span class="line">2、加上wraps 可以保留原有的函数名不发生变化</span><br></pre></td></tr></table></figure>

<p>39、请给出下面代码片段的输出</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">test</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">try</span>:  </span><br><span class="line"><span class="number">3.</span>	        <span class="keyword">raise</span> ValueError(<span class="string">"something wrong"</span>)  </span><br><span class="line"><span class="number">4.</span>	    <span class="keyword">except</span> ValueError <span class="keyword">as</span> e:  </span><br><span class="line"><span class="number">5.</span>	        print(<span class="string">"Error occurred"</span>)  </span><br><span class="line"><span class="number">6.</span>	        <span class="keyword">return</span>  </span><br><span class="line"><span class="number">7.</span>	    <span class="keyword">finally</span>:  </span><br><span class="line"><span class="number">8.</span>	        print(<span class="string">"Done"</span>)  </span><br><span class="line"><span class="number">9.</span>	  </span><br><span class="line"><span class="number">10.</span>	test()  </span><br><span class="line">    Error occurred</span><br><span class="line">    Don</span><br></pre></td></tr></table></figure>

<p>40、下面的函数,那些会输出1,2,3三个数字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">3</span>):  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">print</span> i  </span><br><span class="line"><span class="number">3.</span>	    alist = [<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>]  </span><br><span class="line"><span class="number">4.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> alist:  </span><br><span class="line"><span class="number">5.</span>	    <span class="keyword">print</span> i+<span class="number">1</span>   会</span><br><span class="line"><span class="number">6.</span>	i = <span class="number">1</span>  </span><br><span class="line"><span class="number">7.</span>	<span class="keyword">while</span> i&lt;<span class="number">3</span>:  </span><br><span class="line"><span class="number">8.</span>	    <span class="keyword">print</span> i  </span><br><span class="line"><span class="number">9.</span>	    i+=<span class="number">1</span>  </span><br><span class="line"><span class="number">10.</span>	  </span><br><span class="line"><span class="number">11.</span>	<span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">3</span>):  </span><br><span class="line"><span class="number">12.</span>	    <span class="keyword">print</span> i+<span class="number">1</span>   <span class="comment">#会</span></span><br></pre></td></tr></table></figure>

<p>41  以下函数需要在其中引用一个全局变量k, 请填写语句</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">fun</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	   __________  </span><br><span class="line"><span class="number">3.</span>	   k = k+<span class="number">1</span>  </span><br><span class="line"><span class="comment"># global k</span></span><br></pre></td></tr></table></figure>

<p>42、请把以下函数转化为python lambda匿名函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1. def add(x,y):  </span><br><span class="line">2.     return x+y</span><br><span class="line">lambda x,y:x+y</span><br></pre></td></tr></table></figure>

<p>43 阅读以下代码, 并写出程序的输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">my_dict = &#123;<span class="string">"a"</span>:<span class="number">0</span>,<span class="string">"b"</span>:<span class="number">1</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">(d)</span>:</span></span><br><span class="line">    d[<span class="string">"a"</span>]=<span class="number">1</span></span><br><span class="line">    <span class="keyword">return</span> d</span><br><span class="line"></span><br><span class="line">func(my_dict)</span><br><span class="line">my_dict[<span class="string">"c"</span>]=<span class="number">2</span>  </span><br><span class="line">print(my_dict)</span><br><span class="line">&#123;<span class="string">'a'</span>: <span class="number">1</span>, <span class="string">'b'</span>: <span class="number">1</span>, <span class="string">'c'</span>: <span class="number">2</span>&#125;</span><br></pre></td></tr></table></figure>

<p>44 填空题</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="comment"># 有函数定义如下  </span></span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">calc</span><span class="params">(a,b,c,d=<span class="number">1</span>,e=<span class="number">2</span>)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	    <span class="keyword">return</span> (a+b)*(c-d)+e  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	<span class="comment"># 请分别写出以下标号代码的输出结果, 如果出错请写出Error  </span></span><br><span class="line"><span class="number">6.</span>	<span class="keyword">print</span> calc(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>) <span class="comment"># __4__  </span></span><br><span class="line"><span class="number">7.</span>	<span class="keyword">print</span> calc(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>) <span class="comment"># ___8_  </span></span><br><span class="line"><span class="number">8.</span>	<span class="keyword">print</span> calc(<span class="number">1</span>,<span class="number">2</span>) <span class="comment"># _报错___  </span></span><br><span class="line"><span class="number">9.</span>	<span class="keyword">print</span> calc(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,e=<span class="number">4</span>) <span class="comment"># ____10  </span></span><br><span class="line"><span class="number">10.</span>	<span class="keyword">print</span> calc(e=<span class="number">4</span>, c=<span class="number">5</span>, a=<span class="number">2</span>,b=<span class="number">3</span>) <span class="comment"># _24___  </span></span><br><span class="line"><span class="number">11.</span>	<span class="keyword">print</span> calc(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>, d=<span class="number">5</span>,<span class="number">4</span>) <span class="comment"># 报错___</span></span><br></pre></td></tr></table></figure>

<p>45、def(a,b=[])这种写法有什么陷阱？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">这种陷阱就是默认参数b=[],在调用这个函数没有传新的参数进来,用的都是b这个列表.</span><br></pre></td></tr></table></figure>

<p>46、函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">add_end</span><span class="params">(l=[])</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    l.append(<span class="string">"end"</span>)  </span><br><span class="line"><span class="number">3.</span>	    <span class="keyword">return</span> l  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	add_end() <span class="comment"># 输出什么  l=["end"]</span></span><br><span class="line"><span class="number">6.</span>	add_end() <span class="comment"># 再次调用输出什么? 为什么  l=["end","end"]</span></span><br><span class="line"><span class="comment">#因为是默认列表引用的是内存地址，往可变的内存地址里添加了两次end</span></span><br></pre></td></tr></table></figure>

<p>47、函数参数 <em>args,*</em>kwargs的作用是什么</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">args动态接收位置参数</span><br><span class="line">** kwargs动态接收关键字参数</span><br></pre></td></tr></table></figure>

<p>48、可变参数定义 <em>args,*</em>kwargs的区别是什么?并且写出下边代码的输入内容</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">(*args,**kwargs)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    print(<span class="string">"args="</span>, args)  </span><br><span class="line"><span class="number">3.</span>	    print(<span class="string">"kwargs="</span>,kwargs)  </span><br><span class="line"><span class="number">4.</span>	    print(<span class="string">"-----------------"</span>)  </span><br><span class="line"><span class="number">5.</span>	      </span><br><span class="line"><span class="number">6.</span>	<span class="keyword">if</span> __name__ ==<span class="string">'__main__'</span>:  </span><br><span class="line"><span class="number">7.</span>	    foo(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>)  <span class="comment">#args= (1, 2, 3, 4)  kwargs= &#123;&#125;</span></span><br><span class="line"><span class="number">8.</span>	    foo(a=<span class="number">1</span>,b=<span class="number">2</span>,c=<span class="number">3</span>)  <span class="comment">#args= ()  kwargs= &#123;"a":1,"b":2,"c":3&#125;</span></span><br><span class="line"><span class="number">9.</span>	    foo(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,a=<span class="number">1</span>,b=<span class="number">2</span>,c=<span class="number">3</span>)   <span class="comment">#args= (1, 2, 3, 4)  kwargs= &#123;"a":1,"b":2,"c":3&#125;</span></span><br><span class="line"><span class="number">10.</span>	    foo(<span class="string">"a"</span>,<span class="number">1</span>,<span class="literal">None</span>,a=<span class="number">1</span>,b=<span class="string">"2"</span>,c=<span class="number">3</span>)  <span class="comment">#args= ('a', 1, None)kwargs= &#123;'a': 1, 'b': '2', 'c': 3&#125;</span></span><br></pre></td></tr></table></figure>

<p>49、请写出log实现(主要功能时打印函数名)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	@log  </span><br><span class="line"><span class="number">2.</span>	<span class="function"><span class="keyword">def</span> <span class="title">now</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	    <span class="keyword">print</span> <span class="string">"2013-12-25"</span>  </span><br><span class="line"><span class="number">4.</span>	  </span><br><span class="line"><span class="number">5.</span>	now()  </span><br><span class="line"><span class="number">6.</span>	输出  </span><br><span class="line"><span class="number">7.</span>	call now()  </span><br><span class="line"><span class="number">8.</span>	<span class="number">2013</span><span class="number">-12</span><span class="number">-25</span> </span><br><span class="line"><span class="comment">#-----------------------------</span></span><br><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">log</span><span class="params">(func)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">func_log</span><span class="params">(*args,**kwargs)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	        print(<span class="string">'call %s()'</span>%func.__name__)  </span><br><span class="line"><span class="number">4.</span>	        func(*args,**kwargs)  </span><br><span class="line"><span class="number">5.</span>	    <span class="keyword">return</span> func_log  </span><br><span class="line"><span class="number">6.</span>	 </span><br><span class="line"><span class="number">7.</span>	@log  </span><br><span class="line"><span class="number">8.</span>	<span class="function"><span class="keyword">def</span> <span class="title">now</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">9.</span>	    print(<span class="string">'2013-12-25'</span>)  </span><br><span class="line"><span class="number">10.</span>	now()</span><br></pre></td></tr></table></figure>

<p>50 Python如何定义一个函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">func</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>

<p>51、代码运行结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	country_counter =&#123;&#125;  </span><br><span class="line"><span class="number">2.</span>	  </span><br><span class="line"><span class="number">3.</span>	<span class="function"><span class="keyword">def</span> <span class="title">addone</span><span class="params">(country)</span>:</span>  </span><br><span class="line"><span class="number">4.</span>	    <span class="keyword">if</span> country <span class="keyword">in</span> country_counter:  </span><br><span class="line"><span class="number">5.</span>	        country_counter[country ]+=<span class="number">1</span>  </span><br><span class="line"><span class="number">6.</span>	    <span class="keyword">else</span>:  </span><br><span class="line"><span class="number">7.</span>	        country_counter[country ]= <span class="number">1</span>  </span><br><span class="line"><span class="number">8.</span>	  </span><br><span class="line"><span class="number">9.</span>	addone(<span class="string">"China"</span>)  </span><br><span class="line"><span class="number">10.</span>	addone(<span class="string">"Japan"</span>)  </span><br><span class="line"><span class="number">11.</span>	addone(<span class="string">"china"</span>)  </span><br><span class="line"><span class="number">12.</span>	<span class="keyword">print</span> len(country_counter ) <span class="comment">#3</span></span><br></pre></td></tr></table></figure>

<p>52、选择输出结果</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">doff</span><span class="params">(arg1,*args)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">print</span> type(args)  </span><br><span class="line"><span class="number">3.</span>	  </span><br><span class="line"><span class="number">4.</span>	doff(<span class="string">"applea"</span>,<span class="string">"bananas"</span>,<span class="string">"cherry"</span>)</span><br><span class="line"><span class="comment">#元组</span></span><br></pre></td></tr></table></figure>

<p>53、下面程序的输出结</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	d = <span class="keyword">lambda</span> p:p*<span class="number">2</span>  </span><br><span class="line"><span class="number">2.</span>	t = <span class="keyword">lambda</span> p:p*<span class="number">3</span>  </span><br><span class="line"><span class="number">3.</span>	  </span><br><span class="line"><span class="number">4.</span>	x = <span class="number">2</span>  </span><br><span class="line"><span class="number">5.</span>	x = d(x)  <span class="comment">#4</span></span><br><span class="line"><span class="number">6.</span>	x = t(x)  <span class="comment">#12</span></span><br><span class="line"><span class="number">7.</span>	x = d(x)  <span class="comment">#24</span></span><br><span class="line"><span class="number">8.</span>	<span class="keyword">print</span> x <span class="number">24</span></span><br></pre></td></tr></table></figure>

<p>54、什么是lambda表达式？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">：匿名函数,一行代码的函数</span><br></pre></td></tr></table></figure>

<p>55、以下代码输出是什么,请给出答案并解释</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">multipliers</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">return</span> [<span class="keyword">lambda</span> x:x*i <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">4</span>)]  </span><br><span class="line"><span class="number">3.</span>	  </span><br><span class="line"><span class="number">4.</span>	print([m(<span class="number">2</span>) <span class="keyword">for</span> m <span class="keyword">in</span> multipliers()])</span><br><span class="line">结果：[<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>,<span class="number">6</span>]</span><br></pre></td></tr></table></figure>

<p>56、有 0 &lt; x&lt;= 10, 10 &lt; x &lt;= 20, 20 &lt; x &lt;= 30, .，190 &lt; x〈= 200,200 &lt; x这样的21个区间分别对应1-21二十一个级别，请编写一个函数 level (x)根据输入数值返回对应级别。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">level</span><span class="params">(num)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="keyword">if</span> num % <span class="number">10</span> == <span class="number">0</span>:  </span><br><span class="line"><span class="number">3.</span>	        count = num // <span class="number">10</span> - <span class="number">1</span>  </span><br><span class="line"><span class="number">4.</span>	    <span class="keyword">else</span>:  </span><br><span class="line"><span class="number">5.</span>	        count = num // <span class="number">10</span>  </span><br><span class="line"><span class="number">6.</span>	    print(<span class="string">f'您输入的<span class="subst">&#123;num&#125;</span>级别:<span class="subst">&#123;count&#125;</span>'</span>)  </span><br><span class="line"><span class="number">7.</span>	  </span><br><span class="line"><span class="number">8.</span>	  </span><br><span class="line"><span class="number">9.</span>	level(<span class="number">30</span>)</span><br></pre></td></tr></table></figure>

<p>57、有一个数据结构如下所示，请编写一个函数从该结构数据中返画由指定的 字段和对应的值组成的字典。如果指定字段不存在，则跳过该字段</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	data:&#123;  </span><br><span class="line"><span class="number">2.</span>	    <span class="string">"time"</span>:<span class="string">"2016-08-05T13:13:05"</span>,  </span><br><span class="line"><span class="number">3.</span>	    <span class="string">"some_id"</span>:<span class="string">"ID1234"</span>,  </span><br><span class="line"><span class="number">4.</span>	    <span class="string">"grp1"</span>:&#123;<span class="string">"fld1"</span>:<span class="number">1</span>, <span class="string">"fld2"</span>:<span class="number">2</span>,&#125;,  </span><br><span class="line"><span class="number">5.</span>	    <span class="string">"xxx2"</span>:&#123;<span class="string">"fld3"</span>:<span class="number">0</span>, <span class="string">"fld4"</span>:<span class="number">0.4</span>,&#125;,  </span><br><span class="line"><span class="number">6.</span>	    <span class="string">"fld6"</span>:<span class="number">11</span>,  </span><br><span class="line"><span class="number">7.</span>	    <span class="string">"fld7"</span>: <span class="number">7</span>,  </span><br><span class="line"><span class="number">8.</span>	    <span class="string">"fld46"</span>:<span class="number">8</span>  </span><br><span class="line"><span class="number">9.</span>	&#125;  </span><br><span class="line"><span class="number">10.</span>	  </span><br><span class="line"><span class="number">11.</span>	fields:由<span class="string">"|"</span>连接的以fld开头的字符串, 如fld2|fld7|fld29  </span><br><span class="line"><span class="number">12.</span>	  </span><br><span class="line"><span class="number">13.</span>	<span class="function"><span class="keyword">def</span> <span class="title">select</span><span class="params">(data,fields)</span>:</span>  </span><br><span class="line"><span class="number">14.</span>	    <span class="keyword">return</span> result  </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">select</span><span class="params">(data, f)</span>:</span>  </span><br><span class="line"><span class="number">11.</span>	    result=&#123;&#125;  </span><br><span class="line"><span class="number">12.</span>	    lst = f.split(<span class="string">"|"</span>)  </span><br><span class="line"><span class="number">13.</span>	    <span class="keyword">for</span> k, v <span class="keyword">in</span> data.items():  </span><br><span class="line"><span class="number">14.</span>	        <span class="keyword">for</span> item <span class="keyword">in</span> lst:  </span><br><span class="line"><span class="number">15.</span>	            <span class="keyword">if</span> k == item:  </span><br><span class="line"><span class="number">16.</span>	                result[k] = v  </span><br><span class="line"><span class="number">17.</span>	                <span class="keyword">break</span>  </span><br><span class="line"><span class="number">18.</span>	            <span class="keyword">if</span> type(v) == dict:  </span><br><span class="line"><span class="number">19.</span>	                d = select(v, f) <span class="comment"># 内部小字典的处理  </span></span><br><span class="line"><span class="number">20.</span>	                result.update(d) <span class="comment"># 更新字典  </span></span><br><span class="line"><span class="number">21.</span>	    <span class="keyword">return</span> result  </span><br><span class="line"><span class="number">22.</span>	ret = select(data, <span class="string">"fld1|fld6|fld7"</span>)  </span><br><span class="line"><span class="number">23.</span>	  </span><br><span class="line"><span class="number">24.</span>	print(ret)</span><br></pre></td></tr></table></figure>

<p>58、若要将N个task分配给N个worker同时去完成, 每个worker分别都可以承担这N个task,但费用不同. 下面的程序用回溯法计算总费用最小的一种工作分配方案,<br>在该方案中,<br>为每个worker分配1个task.程序中,N个task从0开始顺序编号, N个worker也从0开始顺序编号, 主要的变量说明如下:</p>
<ul>
<li><p>ci:将任务i分配给worker j的费用</p>
</li>
<li><p>task[i]: 值为0表示task i未分配, 值为j表示task,i分配给worker j</p>
</li>
<li><p>worker[k] 值为0表示未分配task, 值为1表示worker k已分配task;</p>
</li>
<li><p>mincost: 最小总费用</p>
</li>
</ul>
<p>程序</p>
<p>  写个函数接收一个文件夹名称作为参数, 显示文件夹中文件的路径, 以及其中包含文件夹中文件的路径。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	    N=<span class="number">8</span>  </span><br><span class="line"><span class="number">2.</span>	    mincosr = <span class="number">65535</span>  </span><br><span class="line"><span class="number">3.</span>	    worker = []  </span><br><span class="line"><span class="number">4.</span>	    task = []  </span><br><span class="line"><span class="number">5.</span>	    temp = []  </span><br><span class="line"><span class="number">6.</span>	    c = []  </span><br><span class="line"><span class="number">7.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">plan</span><span class="params">(k, cost)</span>:</span>  </span><br><span class="line"><span class="number">8.</span>	        <span class="keyword">global</span>  mincosr  </span><br><span class="line"><span class="number">9.</span>	        <span class="keyword">if</span> __(<span class="number">1</span>)__ <span class="keyword">and</span> cost&lt;mincosr:  </span><br><span class="line"><span class="number">10.</span>	            mincosr = cost  </span><br><span class="line"><span class="number">11.</span>	            <span class="keyword">for</span> i <span class="keyword">in</span> xrange(N):  </span><br><span class="line"><span class="number">12.</span>	                temp[i] = task[i]  </span><br><span class="line"><span class="number">13.</span>	        <span class="keyword">else</span>:  </span><br><span class="line"><span class="number">14.</span>	            <span class="keyword">for</span> i <span class="keyword">in</span> xrange(N):  </span><br><span class="line"><span class="number">15.</span>	                <span class="keyword">if</span> worker[i] ==<span class="number">0</span> <span class="keyword">and</span> __(<span class="number">2</span>)__:  </span><br><span class="line"><span class="number">16.</span>	                    worker[i] = <span class="number">1</span>  </span><br><span class="line"><span class="number">17.</span>	                    task[k] = __(<span class="number">3</span>)__  </span><br><span class="line"><span class="number">18.</span>	                    plan(__(<span class="number">4</span>)__,cost+c[k][i])  </span><br><span class="line"><span class="number">19.</span>	                    __(<span class="number">5</span>)__  </span><br><span class="line"><span class="number">20.</span>	                    task[k] = <span class="number">0</span>  </span><br><span class="line"><span class="number">21.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">22.</span>	        <span class="keyword">for</span> i <span class="keyword">in</span> xrange(N):  </span><br><span class="line"><span class="number">23.</span>	            worker.append(<span class="number">0</span>)  </span><br><span class="line"><span class="number">24.</span>	            task.append(<span class="number">0</span>)  </span><br><span class="line"><span class="number">25.</span>	            temp.append(<span class="number">0</span>)  </span><br><span class="line"><span class="number">26.</span>	            c.append(<span class="number">0</span>)  </span><br><span class="line"><span class="number">27.</span>	            <span class="keyword">for</span> j <span class="keyword">in</span> xrange(N):  </span><br><span class="line"><span class="number">28.</span>	                <span class="keyword">print</span> <span class="string">"请输入 worker"</span>+str(i)+<span class="string">"完成 task"</span> + str(j)+<span class="string">"的花费"</span>  </span><br><span class="line"><span class="number">29.</span>	                input_value = input()  </span><br><span class="line"><span class="number">30.</span>	                c[i].append(int(input_value))  </span><br><span class="line"><span class="number">31.</span>	    plan(<span class="number">0</span>,<span class="number">0</span>)  </span><br><span class="line"><span class="number">32.</span>	    print(<span class="string">'\n 最小费用: '</span>+str(mincosr))  </span><br><span class="line"><span class="number">33.</span>	    <span class="keyword">for</span> i <span class="keyword">in</span> xrange(N):  </span><br><span class="line"><span class="number">34.</span>	        <span class="keyword">print</span> <span class="string">"Task"</span>+str(i)+<span class="string">"is assigned to Worker"</span> + str(temp[i])  </span><br><span class="line"><span class="number">35.</span>	    <span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:  </span><br><span class="line"><span class="number">36.</span>	        main()</span><br></pre></td></tr></table></figure>

<h2 id="第三章-模块"><a href="#第三章-模块" class="headerlink" title="第三章 模块"></a>第三章 模块</h2><p>1、列举常用的模块</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">os  re  sys  time  datetime  collection  json  pickle  functools  requests  logging</span><br></pre></td></tr></table></figure>

<p>2、如何安装第三方模块？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">pip </span><br><span class="line">pycharm</span><br><span class="line">源码安装</span><br></pre></td></tr></table></figure>

<p>3、re 的match和search的区别</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">match是从字符串的头开始查找,search是从字符串中查找</span><br></pre></td></tr></table></figure>

<p>4、什么是正则的贪婪匹配？或 正则匹配中的贪婪模式与非贪婪模式的区别？</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">正则贪婪匹配就是匹配多个,贪婪模式是*号匹配0个或多个 +匹配1个或多个 ,非贪婪是的? 匹配0个或1个</span><br></pre></td></tr></table></figure>

<p>5、如何生成一个随机数？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">使用random 模块</span><br></pre></td></tr></table></figure>

<p>6、  如何使用python删除一个文件？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">使用os.remove()</span><br></pre></td></tr></table></figure>

<p>7、logging模块的作用？以及应用场景？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">logging模块的作用就是记录程序执行出现的问题,</span><br><span class="line">应用场景 我们可以为了方便下一人能够快速找到程序的bug</span><br></pre></td></tr></table></figure>

<p>8、json序列化时，可以处理的数据类型有哪些？如何定制支持datetime类型？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"> string、int、list、tuple、dict、bool、null</span><br><span class="line"><span class="number">4.</span>	<span class="keyword">import</span> json  </span><br><span class="line"><span class="number">5.</span>	<span class="keyword">from</span> json <span class="keyword">import</span> JSONEncoder  </span><br><span class="line"><span class="number">6.</span>	<span class="keyword">from</span> datetime <span class="keyword">import</span> datetime  </span><br><span class="line"><span class="number">7.</span>	<span class="class"><span class="keyword">class</span> <span class="title">ComplexEncoder</span><span class="params">(JSONEncoder)</span>:</span>  </span><br><span class="line"><span class="number">8.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">default</span><span class="params">(self, obj)</span>:</span>  </span><br><span class="line"><span class="number">9.</span>	        <span class="keyword">if</span> isinstance(obj, datetime):  </span><br><span class="line"><span class="number">10.</span>	            <span class="keyword">return</span> obj.strftime(‘%Y-%m-%d %H:%M:%S‘)  </span><br><span class="line"><span class="number">11.</span>	        <span class="keyword">else</span>:  </span><br><span class="line"><span class="number">12.</span>	            <span class="keyword">return</span> super(ComplexEncoder,self).default(obj)  </span><br><span class="line"><span class="number">13.</span>	d = &#123; ‘name‘:‘alex‘,‘data‘:datetime.now()&#125;  </span><br><span class="line"><span class="number">14.</span>	print(json.dumps(d,cls=ComplexEncoder))  </span><br><span class="line"><span class="number">15.</span>	<span class="comment"># &#123;"name": "alex", "data": "2018-05-18 19:52:05"&#125;</span></span><br></pre></td></tr></table></figure>

<p>9、json序列化时，默认遇到中文会转换成unicode，如果想要保留中文怎么办？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1.	print(json.dumps(&apos;你好啊&apos;,ensure_ascii=False))</span><br></pre></td></tr></table></figure>

<p>10、写代码实现查看一个目录下的所有文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1.	import os  </span><br><span class="line">2.	print(os.listdir(r&apos;D:\Python_project\untitled2&apos;))</span><br></pre></td></tr></table></figure>

<p>11、用Python匹配HTML tag的时候,&lt;.<em>&gt;**和&lt;.</em>?&gt;有什么区别?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;.&gt; 匹配是一个任意的内容</span><br><span class="line">&lt;.?&gt;匹配的零个或多个任意的内容</span><br></pre></td></tr></table></figure>

<p>12 如何判断一个邮箱合法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	\w[-\w.+]*@([A-Za-z0<span class="number">-9</span>][-A-Za-z0<span class="number">-9</span>]+\.)+[A-Za-z]&#123;<span class="number">2</span>,<span class="number">14</span>&#125;</span><br></pre></td></tr></table></figure>

<p>13、请写出以字母或下划线开始,以数字结束的正则表达式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">^[_a-zA-Z]*\d$</span><br></pre></td></tr></table></figure>

<p>14、下面那些是Python开发网络应用的框架</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DJango Flask  Web2py Tornado</span><br></pre></td></tr></table></figure>

<p>15、 写Python爬虫都用到了那些模块, 分别是做什么用的?</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">bs4 进行页面解析,将str当成html来操作</span><br><span class="line">re 匹配一个标签和关键字</span><br><span class="line">requests 想服务器发送请求</span><br></pre></td></tr></table></figure>

<p>16、sys.path.append(“/root/mods”)的作用？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">作用是将root/moods/目录添加到自定义模块加载的路径中</span><br></pre></td></tr></table></figure>

<p>17、列举出Python中比较熟知的爬虫框架</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Scrapy</span><br><span class="line">selenium</span><br><span class="line">Crawley</span><br><span class="line">Portia</span><br><span class="line">newpaper</span><br></pre></td></tr></table></figure>

<p>18、输入某年某日, 判断这是这一年的第几天?(可以用Python的内置模块)</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="keyword">import</span> datetime  </span><br><span class="line"><span class="number">2.</span>	d = datetime.datetime(<span class="number">2019</span>,<span class="number">2</span>,<span class="number">1</span>)  </span><br><span class="line"><span class="number">3.</span>	print(d.strftime(<span class="string">"%j"</span>))</span><br></pre></td></tr></table></figure>

<p>19、使用过Python那些第三方组件？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Django</span><br><span class="line">pymysql</span><br><span class="line">SQLalchemy</span><br><span class="line">bs4</span><br><span class="line">pyecharts</span><br><span class="line">PIL</span><br><span class="line">gevent</span><br><span class="line">jieba</span><br><span class="line">selenium</span><br></pre></td></tr></table></figure>

<h2 id="第四章-面向对象"><a href="#第四章-面向对象" class="headerlink" title="第四章 面向对象"></a>第四章 面向对象</h2><p>1、简述面向对象的三大特性</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">面对对象是一种编程思想，将有共同的属性和方法的事物封装到同一个类下面。</span><br><span class="line">继承：</span><br><span class="line">	将多个类的共同属性和方法封装到一个父类下面，然后在用这些类来继承这个类的属性和方法</span><br><span class="line">封装：</span><br><span class="line">	将有共同的属性和方法封装到同一个类下面</span><br><span class="line">	第一层面：</span><br><span class="line">	创建类和对象会分别创建二者的名称空间，我们只能用类名.或者obj.的方式去访问里面的名字，这本身就是一种封装</span><br><span class="line">	第二层面：</span><br><span class="line">    类中把某些属性和方法隐藏起来(或者说定义成私有的)，只在类的内部使用、外部无法访问，或者留下少量接口（函数）供外部访问。</span><br><span class="line">多态：</span><br><span class="line">	Python天生是支持多态的。指的是基类的同一个方法在不同的派生类中有着不同的功能</span><br></pre></td></tr></table></figure>

<p>2、什么是鸭子模型</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">有多个类,它们没有继承关联关系,但是定义的方法和调用方式相同,这就是鸭子类型.遵守了同样的方式</span><br><span class="line">与多肽的区别有无继承。</span><br></pre></td></tr></table></figure>

<p>3、super的作用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">能够自动找到要继承的类,使用super就可以省去自己定义要继承那个类</span><br></pre></td></tr></table></figure>

<p>4、mro是什么</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MRO是一种算法 也是(方法解析顺序),继承顺序</span><br></pre></td></tr></table></figure>

<p>5、什么是c3算法?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">c3算法是多重继承的一种算法,使用了c3就是生成一个线性序列</span><br></pre></td></tr></table></figure>

<p>6、列举面向对象中带双下划线的特殊方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">__new__  生成实例</span><br><span class="line">__init__ 实例化</span><br><span class="line">__call__ 实例对象加( )会执行def __call__:... 方法里边的内容。</span><br><span class="line">_del__：析构方法，当对象在内存中被释放时，自动触发执行。如当 del obj 或者应用程序运行完毕时，执行该方法里边的内容。</span><br><span class="line"></span><br><span class="line">__enter__和__exit__：出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量；with中代码块执行完毕时执行__exit__里边的内容。</span><br><span class="line"></span><br><span class="line">__module__：表示当前操作的对象在那个模块   obj.__module__</span><br><span class="line">__class__ ：表示当前操作的对象的类是什么     obj.__class__</span><br><span class="line"></span><br><span class="line">__doc__：类的描述信息，该描述信息无法被继承</span><br><span class="line"></span><br><span class="line">__str__：改变对象的字符串显示 print函数 ---&gt;obj.__str__()</span><br><span class="line">__repr__：改变对象的字符串显示 交互式解释器 ---&gt;obj.__repr__()</span><br><span class="line">__format__：自定制格式化字符串</span><br><span class="line"></span><br><span class="line">__slots__:一个类变量 用来限制实例可以添加的属性的数量和类型  </span><br><span class="line"></span><br><span class="line">__setitem__,__getitem,__delitem__:</span><br></pre></td></tr></table></figure>

<p>7、双下划线和单下划线的区别？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">双下划线是程序中规定的私有的</span><br><span class="line">单下划线是程序员们约定这是一种私有的</span><br></pre></td></tr></table></figure>

<p>8、实例变量和类变量的区别？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#实例变量就是将我们设定的变量存储在实例开辟的一块空间中</span></span><br><span class="line"><span class="comment">#类变量是将我们设定的变量存储在类开辟的一块空间中,他们的区别就是不是同一块空间</span></span><br></pre></td></tr></table></figure>

<p>9、静态方法和类方法区别？</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#classmethod 必须有一个指向类对象的引用作为第一个参数</span><br><span class="line">#staticmethod 可以没有任何参数。</span><br></pre></td></tr></table></figure>

<p>10、isinstance和type的作用？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">isinstance是判断某个对象是什么类型（支持继承）</span><br><span class="line">type是查看某个对象是什么类型(不支持继承)</span><br><span class="line">EG:</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span><span class="params">(A)</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">b = B()</span><br><span class="line">print(isinstance(b,B)) <span class="comment">#True</span></span><br><span class="line">print(isinstance(b,A))<span class="comment">#True</span></span><br><span class="line">print(type(b) ==B)<span class="comment">#True</span></span><br><span class="line">print(type(b) ==A)<span class="comment">#False</span></span><br></pre></td></tr></table></figure>

<p>11、有用过with statement（语句）吗？它的好处是什么？</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">用过 <span class="keyword">with</span> open 它的好处就是操作文件的时候不需要自己手动关闭文件</span><br><span class="line">它使用了python面向对象中上下问管理系统</span><br></pre></td></tr></table></figure>

<p>12、下列数据结构中,哪一种是不可迭代的</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">不可迭代：object</span><br><span class="line">可迭代的：dict set str</span><br></pre></td></tr></table></figure>

<p>13、实现一个Singleton单例类, 要求遵循基本语言编程规范（用尽量多的方式）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">1、python</span><br><span class="line">#Python的模块就是天然的单例模式</span><br><span class="line">#模块在第一次导入时，会生成 .pyc 文件，当第二次导入时，就会直接加载 .pyc 文件，而不会再次执行模块代码</span><br><span class="line">#我们只需把相关的函数和数据定义在一个模块中，就可以获得一个单例对象了</span><br><span class="line">eg:</span><br><span class="line">a.py</span><br><span class="line">class V1(object):</span><br><span class="line">    def foo(self)</span><br><span class="line">        pass</span><br><span class="line">V1 = V1()</span><br><span class="line">如：from a import V1</span><br><span class="line"></span><br><span class="line">2、使用装饰器</span><br><span class="line">def Singleton(cls):</span><br><span class="line">    _instance = &#123;&#125;</span><br><span class="line">    def _singleton(*args, **kargs):</span><br><span class="line">        if cls not in _instance:</span><br><span class="line">            _instance[cls] = cls(*args, **kargs)</span><br><span class="line">        return _instance[cls]</span><br><span class="line">    return _singleton</span><br><span class="line">@Singleton</span><br><span class="line">class A(object):</span><br><span class="line">    a = 1</span><br><span class="line">    def __init__(self, x=0):</span><br><span class="line">        self.x = x</span><br><span class="line">a1 = A(2)</span><br><span class="line">a2 = A(3)</span><br><span class="line"></span><br><span class="line">3、使用类</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">4、基于__new__方法实现</span><br><span class="line">class Singleton:</span><br><span class="line">    def __new__(cls, *args, **kw):</span><br><span class="line">        if not hasattr(cls, &apos;_instance&apos;):</span><br><span class="line">            cls._instance = object.__new__(cls)</span><br><span class="line">        return cls._instance</span><br></pre></td></tr></table></figure>

<p>14、请描述with的用法, 如果自己的类需要支持with语句, 应该如何书写?</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span>的用法就是 <span class="keyword">with</span>一个关键字 后边跟上自己定义的函数,然后<span class="keyword">as</span>去赋值一个句柄</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Myfile</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,path,mode=<span class="string">'r'</span>,encoding = <span class="string">'utf-8'</span>)</span>:</span></span><br><span class="line">        self.path = path</span><br><span class="line">        self.mode = mode</span><br><span class="line">        self.encoding = encoding</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__enter__</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.f = open(self.path, mode=self.mode, encoding=self.encoding)</span><br><span class="line">        <span class="keyword">return</span> self.f</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__exit__</span><span class="params">(self, exc_type, exc_val, exc_tb)</span>:</span></span><br><span class="line">        self.f.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> Myfile(<span class="string">'file'</span>,mode=<span class="string">'w'</span>) <span class="keyword">as</span> f:</span><br><span class="line">    f.write(<span class="string">'wahaha'</span>)</span><br><span class="line"><span class="comment">#with open as 的机制</span></span><br></pre></td></tr></table></figure>

<p>15、python中如何判断一个对象是否可调用?那些对象可以是可调用对象?如何定义一个类, 使其对象本身就是可调用对象?</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#callable判断一个对象是否可调用</span></span><br><span class="line"><span class="comment">#实现 __call__方法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__call__</span><span class="params">(self, *args, **kwargs)</span>:</span></span><br><span class="line">        print(<span class="string">"A被调用了"</span>)</span><br><span class="line">a =A()</span><br><span class="line">print(callable(a)) <span class="comment">#True</span></span><br><span class="line">a()<span class="comment">#A被调用了</span></span><br></pre></td></tr></table></figure>

<p>16、实现一个栈</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stack</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,limit =<span class="number">10</span>)</span>:</span></span><br><span class="line">        self.__stack =[]</span><br><span class="line">        self.__limit =limit</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__str__</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">""</span>.join([str(i) <span class="keyword">for</span> i <span class="keyword">in</span> self.__stack])</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">push</span><span class="params">(self,data)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> len(self.__stack)&gt;=self.__limit:</span><br><span class="line">            self.double_limit()</span><br><span class="line">        self.__stack.append(data)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">pop</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> len(self.__stack)&lt;=<span class="number">0</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> self.__stack.pop()</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">peek</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> len(self.__stack)&lt;=<span class="number">0</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> self.__stack[<span class="number">-1</span>]</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">is_empty</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> self.size()==<span class="number">0</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">size</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> len(self.size())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">double_limit</span><span class="params">(self)</span>:</span></span><br><span class="line">        self.__limit *=<span class="number">2</span></span><br><span class="line"></span><br><span class="line">st =Stack()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">10</span>):</span><br><span class="line">    st.push(i)</span><br><span class="line">print(st)</span><br><span class="line">st.pop()</span><br><span class="line">print(st)</span><br><span class="line">print(st.peek())</span><br><span class="line"><span class="comment">#思路：</span></span><br><span class="line">首先定义一个类，用于存放元素，和类的默认大小</span><br><span class="line">添加和删除元素</span><br><span class="line">返回栈顶元素</span><br><span class="line">扩容</span><br><span class="line">求大小</span><br><span class="line">判断是否为空</span><br></pre></td></tr></table></figure>

<p>17、Python类的继承不正确的说法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">APython类无法继承</span><br><span class="line">B.  可以继承, 无法执行父类的构造函数</span><br><span class="line">C.  可以有多个父类</span><br><span class="line">D.  只能有一个父类</span><br><span class="line"><span class="comment">#ABD</span></span><br></pre></td></tr></table></figure>

<p>18、实现一个hashtable类, 对外暴露的有add和get方法, 满足以下测试代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="function"><span class="keyword">def</span> <span class="title">test</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	   <span class="keyword">import</span> uuid  </span><br><span class="line"><span class="number">3.</span>	   name = &#123;<span class="string">"name"</span>, <span class="string">"web"</span>, <span class="string">"python"</span>&#125;  </span><br><span class="line"><span class="number">4.</span>	   ht = HashTable()  </span><br><span class="line"><span class="number">5.</span>	   <span class="keyword">for</span> key <span class="keyword">in</span> names:  </span><br><span class="line"><span class="number">6.</span>	       value = uuid.uuid4()  </span><br><span class="line"><span class="number">7.</span>	       ht.add(key,value)  </span><br><span class="line"><span class="number">8.</span>	       print(<span class="string">"add元素"</span>,key,value)  </span><br><span class="line"><span class="number">9.</span>	  </span><br><span class="line"><span class="number">10.</span>	   <span class="keyword">for</span> key <span class="keyword">in</span> names:  </span><br><span class="line"><span class="number">11.</span>	       v = ht.get(key)  </span><br><span class="line"><span class="number">12.</span>	       print(<span class="string">"get 元素"</span>,key, v) </span><br><span class="line"></span><br><span class="line"><span class="comment">#---------------------------</span></span><br><span class="line"><span class="number">1.</span>	<span class="class"><span class="keyword">class</span> <span class="title">HashTable</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	  </span><br><span class="line"><span class="number">3.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span>  </span><br><span class="line"><span class="number">4.</span>	        self.dic = &#123;&#125;  </span><br><span class="line"><span class="number">5.</span>	  </span><br><span class="line"><span class="number">6.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">add</span><span class="params">(self,key,value)</span>:</span>  </span><br><span class="line"><span class="number">7.</span>	        self.dic[key] = value  </span><br><span class="line"><span class="number">8.</span>	  </span><br><span class="line"><span class="number">9.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">get</span><span class="params">(self,key)</span>:</span>  </span><br><span class="line"><span class="number">10.</span>	        <span class="keyword">return</span> self.dic.get(key)  </span><br><span class="line"><span class="number">11.</span>	  </span><br><span class="line"><span class="number">12.</span>	  </span><br><span class="line"><span class="number">13.</span>	<span class="function"><span class="keyword">def</span> <span class="title">test</span><span class="params">()</span>:</span>  </span><br><span class="line"><span class="number">14.</span>	    <span class="keyword">import</span> uuid  </span><br><span class="line"><span class="number">15.</span>	    names = &#123;<span class="string">'name'</span>,<span class="string">'web'</span>,<span class="string">'python'</span>&#125;  </span><br><span class="line"><span class="number">16.</span>	    ht = HashTable()  </span><br><span class="line"><span class="number">17.</span>	    <span class="keyword">for</span> key <span class="keyword">in</span> names:  </span><br><span class="line"><span class="number">18.</span>	        value = uuid.uuid4()  </span><br><span class="line"><span class="number">19.</span>	        ht.add(key,value)  </span><br><span class="line"><span class="number">20.</span>	        print(<span class="string">'add元素'</span>,key,value)  </span><br><span class="line"><span class="number">21.</span>	  </span><br><span class="line"><span class="number">22.</span>	    <span class="keyword">for</span> key <span class="keyword">in</span> names:  </span><br><span class="line"><span class="number">23.</span>	        v = ht.get(key)  </span><br><span class="line"><span class="number">24.</span>	        print(<span class="string">'get元素'</span>,key,v)  </span><br><span class="line"><span class="number">25.</span>	  </span><br><span class="line"><span class="number">26.</span>	test()</span><br></pre></td></tr></table></figure>

<p>19、请用两个队列来实现一个栈</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="class"><span class="keyword">class</span> <span class="title">StackWithTwoQueues</span><span class="params">(object)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	        self._stack1 = []  </span><br><span class="line"><span class="number">4.</span>	        self._stack2 = []  </span><br><span class="line"><span class="number">5.</span>	  </span><br><span class="line"><span class="number">6.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">push</span><span class="params">(self,x)</span>:</span>  </span><br><span class="line"><span class="number">7.</span>	        <span class="keyword">if</span> len(self._stack1) == <span class="number">0</span>:  </span><br><span class="line"><span class="number">8.</span>	            self._stack1.append(x)  </span><br><span class="line"><span class="number">9.</span>	        <span class="keyword">elif</span> len(self._stack2) == <span class="number">0</span>:  </span><br><span class="line"><span class="number">10.</span>	            self._stack2.append(x)  </span><br><span class="line"><span class="number">11.</span>	        <span class="keyword">if</span> len(self._stack2) == <span class="number">1</span> <span class="keyword">and</span> len(self._stack1) &gt;= <span class="number">1</span>:  </span><br><span class="line"><span class="number">12.</span>	            <span class="keyword">while</span> self._stack1:  </span><br><span class="line"><span class="number">13.</span>	                self._stack2.append(self._stack1.pop(<span class="number">0</span>))  </span><br><span class="line"><span class="number">14.</span>	        <span class="keyword">elif</span> len(self._stack1) == <span class="number">1</span> <span class="keyword">and</span> len(self._stack2) &gt; <span class="number">1</span>:  </span><br><span class="line"><span class="number">15.</span>	            <span class="keyword">while</span> self._stack2:  </span><br><span class="line"><span class="number">16.</span>	                self._stack1.append(self._stack2.pop(<span class="number">0</span>))  </span><br><span class="line"><span class="number">17.</span>	  </span><br><span class="line"><span class="number">18.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">pop</span><span class="params">(self)</span>:</span>  </span><br><span class="line"><span class="number">19.</span>	        <span class="keyword">if</span> self._stack1:  </span><br><span class="line"><span class="number">20.</span>	            <span class="keyword">return</span> self._stack1.pop(<span class="number">0</span>)  </span><br><span class="line"><span class="number">21.</span>	        <span class="keyword">elif</span> self._stack2:  </span><br><span class="line"><span class="number">22.</span>	            <span class="keyword">return</span> self._stack2.pop(<span class="number">0</span>)  </span><br><span class="line"><span class="number">23.</span>	        <span class="keyword">else</span>:  </span><br><span class="line"><span class="number">24.</span>	            <span class="keyword">return</span> <span class="literal">None</span></span><br></pre></td></tr></table></figure>

<p>20、已知如下链表类, 请实现单链表逆（不会）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Node</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,value,next)</span>:</span></span><br><span class="line">        self.value =value</span><br><span class="line">        self.next= next</span><br><span class="line"><span class="comment">#答案</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">reverseList</span><span class="params">(self,pHead)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> pHead <span class="keyword">or</span> <span class="keyword">not</span> pHead.next:</span><br><span class="line">            <span class="keyword">return</span> pHead</span><br><span class="line">        last=<span class="literal">None</span></span><br><span class="line">        <span class="keyword">while</span> pHead:</span><br><span class="line">            tmp=pHead.next</span><br><span class="line">            pHead.next=last</span><br><span class="line">            last=pHead</span><br><span class="line">            pHead=tmp</span><br><span class="line">        <span class="keyword">return</span> last</span><br></pre></td></tr></table></figure>

<p>21、类的加载顺序(类中有继承有构造有静态)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">实例化:在堆内存中生成class对象, 把静态变量和静态方法加载到方法区, 这个堆内存中的class对象是方法区数据的入口</span><br><span class="line">类的加载顺序：</span><br><span class="line">    静态变量默认初始化</span><br><span class="line">    静态变量显式初始化</span><br><span class="line">    执行静态代码块</span><br><span class="line">    成员变量默认初始化, 显示初始化</span><br><span class="line">    执行构造函数</span><br></pre></td></tr></table></figure>

<p>22、参考下面代码片段，请在Context类下添加代码完成该类的实现</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Content</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__enter__</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="string">"before"</span>)</span><br><span class="line">        <span class="keyword">return</span> self  <span class="comment"># 必须写return</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__exit__</span><span class="params">(self, exc_type, exc_val, exc_tb)</span>:</span></span><br><span class="line">        print(<span class="string">"after"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">do_something</span><span class="params">(self)</span>:</span></span><br><span class="line">        print(<span class="number">22</span>)</span><br><span class="line"> </span><br><span class="line"><span class="keyword">with</span> Content() <span class="keyword">as</span> ctx:</span><br><span class="line">    ctx.do_something()</span><br></pre></td></tr></table></figure>

<p>23、以下代码输出是什么? 请给出答案并解释</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="class"><span class="keyword">class</span> <span class="title">Parent</span><span class="params">(object)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    x = <span class="number">1</span>  </span><br><span class="line"><span class="number">3.</span>	  </span><br><span class="line"><span class="number">4.</span>	<span class="class"><span class="keyword">class</span> <span class="title">Child1</span><span class="params">(Parent)</span>:</span>  </span><br><span class="line"><span class="number">5.</span>	    <span class="keyword">pass</span>  </span><br><span class="line"><span class="number">6.</span>	  </span><br><span class="line"><span class="number">7.</span>	<span class="class"><span class="keyword">class</span> <span class="title">Child2</span><span class="params">(Parent)</span>:</span>  </span><br><span class="line"><span class="number">8.</span>	    <span class="keyword">pass</span>  </span><br><span class="line"><span class="number">9.</span>	  </span><br><span class="line"><span class="number">10.</span>	print(Parent.x,Child1.x,Child2.x) <span class="comment">#1,1,1，</span></span><br><span class="line"><span class="number">11.</span>	Child1.x = <span class="number">2</span>  </span><br><span class="line"><span class="number">12.</span>	print(Parent.x,Child1.x,Child2.x)  <span class="comment">#1,1,1</span></span><br><span class="line"><span class="number">13.</span>	Parent.x = <span class="number">3</span>  </span><br><span class="line"><span class="number">14.</span>	print(Parent.x,Child1.x,Child2.x)  <span class="comment">#3,2,3</span></span><br></pre></td></tr></table></figure>

<p>24、函数del_node(self,data)的功能: 在根节点指针为root的二叉树(又称二叉排序树)上排除数值为K的节点,若删除成功,返回0,否则返回-1, 概述节点的定义类型为（没写）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	<span class="class"><span class="keyword">class</span> <span class="title">Node</span><span class="params">(object)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,data)</span>:</span>  </span><br><span class="line"><span class="number">3.</span>	        self.data = data <span class="comment"># 节点的数值  </span></span><br><span class="line"><span class="number">4.</span>	        self.left_child = Node <span class="comment"># 指向左右子树的指针  </span></span><br><span class="line"><span class="number">5.</span>	        self.right_child = Node  </span><br><span class="line"><span class="number">6.</span>	  </span><br><span class="line"><span class="number">7.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">set_data</span><span class="params">(self,data)</span>:</span>  </span><br><span class="line"><span class="number">8.</span>	        self.data = data</span><br></pre></td></tr></table></figure>

<p>25、请给出下面代码片段的输出，请简述上面代码需要改进的地方</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	 <span class="class"><span class="keyword">class</span> <span class="title">Singleton</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    _instance = <span class="literal">None</span>  </span><br><span class="line"><span class="number">3.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">__new__</span><span class="params">(cls, *args, **kwargs)</span>  </span></span><br><span class="line"><span class="function">4.	        <span class="title">print</span><span class="params">(<span class="string">"New"</span>)</span>  </span></span><br><span class="line"><span class="function">5.	        <span class="title">if</span> <span class="title">cls</span>.<span class="title">_instance</span> <span class="title">is</span> <span class="title">None</span>:</span>  </span><br><span class="line"><span class="number">6.</span>	            print(<span class="string">"Create"</span>)  </span><br><span class="line"><span class="number">7.</span>	            cls._instance = super().__new__(cls,*args, **kwargs)  </span><br><span class="line"><span class="number">8.</span>	        <span class="keyword">return</span> cls._instance  </span><br><span class="line"><span class="number">9.</span>	  </span><br><span class="line"><span class="number">10.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self)</span>:</span>  </span><br><span class="line"><span class="number">11.</span>	        print(<span class="string">"Initalize"</span>)  </span><br><span class="line"><span class="number">12.</span>	        self.prop  = <span class="literal">None</span>  </span><br><span class="line"><span class="number">13.</span>	  </span><br><span class="line"><span class="number">14.</span>	s1 = Singleton()  </span><br><span class="line"><span class="number">15.</span>	s2 = singleton()</span><br></pre></td></tr></table></figure>

<p>26、请简单解释Python中的static method(静态方法)和class method(类方法),并将以下代码填写完整</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>	 <span class="class"><span class="keyword">class</span> <span class="title">A</span><span class="params">(object)</span>:</span>  </span><br><span class="line"><span class="number">2.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">foo</span><span class="params">(self,x)</span>  </span></span><br><span class="line"><span class="function">3.	        <span class="title">print</span> '<span class="title">executing</span> <span class="title">foo</span><span class="params">(%s, %s)</span>'%<span class="params">(self,x)</span>  </span></span><br><span class="line"><span class="function">4.	 </span></span><br><span class="line"><span class="function">5.	    @<span class="title">classmethod</span>  </span></span><br><span class="line"><span class="function">6.	    <span class="title">def</span> <span class="title">class_foo</span><span class="params">(cls,x)</span>:</span>  </span><br><span class="line"><span class="number">7.</span>	        <span class="keyword">print</span> <span class="string">'executing class_foo(%s, %s)'</span>%(cls,x)  </span><br><span class="line"><span class="number">8.</span>	 </span><br><span class="line"><span class="number">9.</span>	    @staticmethod  </span><br><span class="line"><span class="number">10.</span>	    <span class="function"><span class="keyword">def</span> <span class="title">static_foo</span><span class="params">(x)</span>:</span>  </span><br><span class="line"><span class="number">11.</span>	        <span class="keyword">print</span> <span class="string">'executing static_foo(%s)'</span>%(x)  </span><br><span class="line"><span class="number">12.</span>	  </span><br><span class="line"><span class="number">13.</span>	a= A()  </span><br><span class="line"><span class="number">14.</span>	<span class="comment"># 调用foo函数,参数传入1  </span></span><br><span class="line"><span class="number">15.</span>	____________________  </span><br><span class="line"><span class="number">16.</span>	<span class="comment"># 调用class_foo函数,参数传入1  </span></span><br><span class="line"><span class="number">17.</span>	____________________  </span><br><span class="line"><span class="number">18.</span>	<span class="comment"># 调用static_foo函数,参数传入1</span></span><br></pre></td></tr></table></figure>

<p>27、已知一个订单对象（tradeOrder）有如下字段：</p>
<table>
<thead>
<tr>
<th align="left">字段英文名</th>
<th>中文名</th>
<th>字段类型</th>
<th>取值</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Id</td>
<td>主键</td>
<td>Long</td>
<td>123456789</td>
</tr>
<tr>
<td align="left">Name</td>
<td>姓名</td>
<td>String</td>
<td>张三</td>
</tr>
<tr>
<td align="left">Items</td>
<td>商品列表集合</td>
<td>List&lt;商品&gt;（关联商品）</td>
<td>查找商品对象，一个订单有两个商品。商品字段任意取值。</td>
</tr>
<tr>
<td align="left">IsMember</td>
<td>是否是会员</td>
<td>Boolean</td>
<td>True</td>
</tr>
<tr>
<td align="left">CouponAmount</td>
<td>优惠券金额</td>
<td>Bigdecimal</td>
<td>Null</td>
</tr>
</tbody></table>
<p>​      商品对象</p>
<table>
<thead>
<tr>
<th>字段英文名称</th>
<th>中文名</th>
<th>字段类型</th>
<th>取值</th>
</tr>
</thead>
<tbody><tr>
<td>Id</td>
<td>主键</td>
<td>Long</td>
<td>987654321</td>
</tr>
<tr>
<td>Name</td>
<td>商品名称</td>
<td>String</td>
<td>手机</td>
</tr>
</tbody></table>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>28、 编程实现一个先进先出的队列类, 能指定初始化时的队列大小, 以及enqueue,dequeue,is<em>empty, is</em>full四种方法</p>
<p><img src="assets/1566787167007.png" alt="1566787167007"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Queue</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self,limit)</span>:</span></span><br><span class="line">        self.items = []</span><br><span class="line">        self.limit = limit</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">isEmpty</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> self.items == []</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">enqueue</span><span class="params">(self, item)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.isful():</span><br><span class="line">            self.items.insert(<span class="number">0</span>,item)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">dequeue</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> self.isEmpty():</span><br><span class="line">            <span class="keyword">return</span> self.items.pop()</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">size</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> len(self.items)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">isful</span><span class="params">(self)</span>:</span></span><br><span class="line">        <span class="keyword">return</span> len(self.items)==self.limit</span><br></pre></td></tr></table></figure>


      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://yoursite.com/2019/09/20/【面试题】01-1绿皮书一/" title="绿皮书01">http://yoursite.com/2019/09/20/【面试题】01-1绿皮书一/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/dxl/tags/面试题/" rel="tag"># 面试题</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/dxl/2019/09/20/【面试题】公司的日常/" rel="next" title="Django">
                <i class="fa fa-chevron-left"></i> Django
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/dxl/2019/09/20/【面试题】01-2绿皮书二/" rel="prev" title="绿皮书02">
                绿皮书02 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/dxl/images/avatar.png" alt>
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/dxl/archives">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/dxl/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/dxl/tags/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-sign-out"></i>
                我的友链
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="tencent://message/?Menu=yes&uin=1258517737&Site=QQ%E6%9E%81%E5%AE%A2&Service=300&sigT=45a1e5847943b64c6ff3990f8a9e644d2b31356cb0b4ac6b24663a3c8dd0f8aa12a595b1714f9d45/" title="申请坑位" target="_blank">申请坑位</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#第一章"><span class="nav-number">1.</span> <span class="nav-text">第一章</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第二章、函数"><span class="nav-number">2.</span> <span class="nav-text">第二章、函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第三章-模块"><span class="nav-number">3.</span> <span class="nav-text">第三章 模块</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#第四章-面向对象"><span class="nav-number">4.</span> <span class="nav-text">第四章 面向对象</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-hand-peace-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder"></span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站总访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/dxl/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/dxl/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/dxl/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/dxl/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/dxl/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/dxl/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/dxl/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/dxl/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: '13B0JGDuA6ttduN8AQaR8CzF-gzGzoHsz',
        appKey: 'I13r9r5mVgq4jQYpYy6V4gW3',
        placeholder: '欢迎大佬指点~~~',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
